mirror of
https://github.com/Michatec/Radio.git
synced 2026-05-31 00:42:40 +02:00
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.
This commit is contained in:
@@ -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
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
createNotificationChannel(context)
|
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
|
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
|
||||||
}
|
}
|
||||||
|
|
||||||
val pendingIntent = PendingIntent.getActivity(
|
val pendingIntent = PendingIntent.getActivity(
|
||||||
context,
|
context,
|
||||||
0,
|
0,
|
||||||
intent,
|
targetIntent,
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,9 +55,9 @@ object NotificationSys {
|
|||||||
notificationManager.notify(NOTIFICATION_ID, notification)
|
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 title = context.getString(titleResId)
|
||||||
val content = context.getString(contentResId)
|
val content = context.getString(contentResId)
|
||||||
showNotification(context, title, content)
|
showNotification(context, title, content, intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,6 +96,11 @@ class PlayerFragment : Fragment(),
|
|||||||
private var tempStationUuid: String = String()
|
private var tempStationUuid: String = String()
|
||||||
private var itemTouchHelper: ItemTouchHelper? = null
|
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 */
|
/* Overrides onCreate from Fragment */
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@@ -850,6 +855,22 @@ class PlayerFragment : Fragment(),
|
|||||||
R.color.default_neutral_white))
|
R.color.default_neutral_white))
|
||||||
.show()
|
.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 ->
|
}, { error ->
|
||||||
Log.w(TAG, "Update check failed", error)
|
Log.w(TAG, "Update check failed", error)
|
||||||
|
|||||||
Reference in New Issue
Block a user