From 2cf98b89b38a9368e0c53bbd19519c7b5f06d477 Mon Sep 17 00:00:00 2001 From: Michatec Date: Sat, 30 May 2026 22:48:26 +0200 Subject: [PATCH] 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. --- .../com/michatec/radio/NotificationSys.kt | 10 ++++----- .../java/com/michatec/radio/PlayerFragment.kt | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/michatec/radio/NotificationSys.kt b/app/src/main/java/com/michatec/radio/NotificationSys.kt index e47dcdd..25cc636 100644 --- a/app/src/main/java/com/michatec/radio/NotificationSys.kt +++ b/app/src/main/java/com/michatec/radio/NotificationSys.kt @@ -28,18 +28,18 @@ object NotificationSys { } } - fun showNotification(context: Context, title: String, content: String) { + fun showNotification(context: Context, title: String, content: String, intent: Intent? = null) { val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager createNotificationChannel(context) - val intent = Intent(context, MainActivity::class.java).apply { + val targetIntent = intent ?: Intent(context, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP } val pendingIntent = PendingIntent.getActivity( context, 0, - intent, + targetIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) @@ -55,9 +55,9 @@ object NotificationSys { notificationManager.notify(NOTIFICATION_ID, notification) } - fun showNotification(context: Context, titleResId: Int, contentResId: Int) { + fun showNotification(context: Context, titleResId: Int, contentResId: Int, intent: Intent? = null) { val title = context.getString(titleResId) val content = context.getString(contentResId) - showNotification(context, title, content) + showNotification(context, title, content, intent) } } \ No newline at end of file diff --git a/app/src/main/java/com/michatec/radio/PlayerFragment.kt b/app/src/main/java/com/michatec/radio/PlayerFragment.kt index cb1adc1..5da5c8b 100644 --- a/app/src/main/java/com/michatec/radio/PlayerFragment.kt +++ b/app/src/main/java/com/michatec/radio/PlayerFragment.kt @@ -96,6 +96,11 @@ class PlayerFragment : Fragment(), private var tempStationUuid: String = String() private var itemTouchHelper: ItemTouchHelper? = null + // Check if the device running the app is an Android TV instance + private val isAndroidTV: Boolean by lazy { + context?.packageManager?.hasSystemFeature(PackageManager.FEATURE_LEANBACK) == true + } + /* Overrides onCreate from Fragment */ override fun onCreate(savedInstanceState: Bundle?) { @@ -850,6 +855,22 @@ class PlayerFragment : Fragment(), R.color.default_neutral_white)) .show() } + + if (!isAndroidTV) { + val releaseUrl = getString(R.string.snackbar_url_app_home_page) + + // Create the clean browser intent that will trigger ONLY when the notification is tapped + val updateIntent = Intent(Intent.ACTION_VIEW, Uri.parse(releaseUrl)).apply { + putExtra("SOURCE", "SELF") + } + + NotificationSys.showNotification( + requireContext(), + "${getString(R.string.app_name)} $latestVersion", + getString(R.string.snackbar_update_available), + intent = updateIntent + ) + } } }, { error -> Log.w(TAG, "Update check failed", error)