Skip to content

Commit 2cf98b8

Browse files
committed
feat(notification): allow custom intent for notifications
Update `NotificationSys` to support passing a custom `Intent` when showing notifications. This enables more flexible navigation, such as opening a web URL for update notifications. In `PlayerFragment`, use this new capability to trigger a browser intent for update notifications on non-Android TV devices.
1 parent 6fa1e5e commit 2cf98b8

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

app/src/main/java/com/michatec/radio/NotificationSys.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ object NotificationSys {
2828
}
2929
}
3030

31-
fun showNotification(context: Context, title: String, content: String) {
31+
fun showNotification(context: Context, title: String, content: String, intent: Intent? = null) {
3232
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
3333
createNotificationChannel(context)
3434

35-
val intent = Intent(context, MainActivity::class.java).apply {
35+
val targetIntent = intent ?: Intent(context, MainActivity::class.java).apply {
3636
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
3737
}
3838

3939
val pendingIntent = PendingIntent.getActivity(
4040
context,
4141
0,
42-
intent,
42+
targetIntent,
4343
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
4444
)
4545

@@ -55,9 +55,9 @@ object NotificationSys {
5555
notificationManager.notify(NOTIFICATION_ID, notification)
5656
}
5757

58-
fun showNotification(context: Context, titleResId: Int, contentResId: Int) {
58+
fun showNotification(context: Context, titleResId: Int, contentResId: Int, intent: Intent? = null) {
5959
val title = context.getString(titleResId)
6060
val content = context.getString(contentResId)
61-
showNotification(context, title, content)
61+
showNotification(context, title, content, intent)
6262
}
6363
}

app/src/main/java/com/michatec/radio/PlayerFragment.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class PlayerFragment : Fragment(),
9696
private var tempStationUuid: String = String()
9797
private var itemTouchHelper: ItemTouchHelper? = null
9898

99+
// Check if the device running the app is an Android TV instance
100+
private val isAndroidTV: Boolean by lazy {
101+
context?.packageManager?.hasSystemFeature(PackageManager.FEATURE_LEANBACK) == true
102+
}
103+
99104

100105
/* Overrides onCreate from Fragment */
101106
override fun onCreate(savedInstanceState: Bundle?) {
@@ -850,6 +855,22 @@ class PlayerFragment : Fragment(),
850855
R.color.default_neutral_white))
851856
.show()
852857
}
858+
859+
if (!isAndroidTV) {
860+
val releaseUrl = getString(R.string.snackbar_url_app_home_page)
861+
862+
// Create the clean browser intent that will trigger ONLY when the notification is tapped
863+
val updateIntent = Intent(Intent.ACTION_VIEW, Uri.parse(releaseUrl)).apply {
864+
putExtra("SOURCE", "SELF")
865+
}
866+
867+
NotificationSys.showNotification(
868+
requireContext(),
869+
"${getString(R.string.app_name)} $latestVersion",
870+
getString(R.string.snackbar_update_available),
871+
intent = updateIntent
872+
)
873+
}
853874
}
854875
}, { error ->
855876
Log.w(TAG, "Update check failed", error)

0 commit comments

Comments
 (0)