From 68a0fd088b6ff1c35c85145349099bd311bb0de5 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 14 Apr 2023 08:20:43 +0530 Subject: [PATCH 1/2] Avoid summary notifications for one stream. --- .../libretube/workers/NotificationWorker.kt | 109 ++++++++++-------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index 6714da900..4df75acff 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -137,68 +137,77 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : * For more information, see https://developer.android.com/develop/ui/views/notifications/group */ private suspend fun createNotificationsForChannel(group: String, streams: List) { - val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or - Intent.FLAG_ACTIVITY_CLEAR_TASK - // Create stream notifications. These are automatically grouped on Android 7.0 and later. - streams.forEach { - val intent = Intent(applicationContext, MainActivity::class.java) - .setFlags(intentFlags) - .putExtra(IntentData.videoId, it.url!!.toID()) - val code = ++notificationId - val pendingIntent = PendingIntentCompat - .getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false) - - val notificationBuilder = createNotificationBuilder(group) - .setContentTitle(it.title) - .setContentText(it.uploaderName) - // The intent that will fire when the user taps the notification - .setContentIntent(pendingIntent) - - // Load stream thumbnails if the relevant toggle is enabled. - if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) { - val thumbnail = withContext(Dispatchers.IO) { - ImageHelper.getImage(applicationContext, it.thumbnail).drawable?.toBitmap() - } - - notificationBuilder - .setLargeIcon(thumbnail) - .setStyle( - NotificationCompat.BigPictureStyle() - .bigPicture(thumbnail) - .bigLargeIcon(null as Bitmap?) // Hides the icon when expanding - ) + if (streams.size == 1) { + showStreamNotification(group, streams[0]) + } else { + streams.forEach { + showStreamNotification(group, it) } - notificationManager.notify(code, notificationBuilder.build()) - } + val summaryId = ++notificationId + val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or + Intent.FLAG_ACTIVITY_CLEAR_TASK + val intent = Intent(applicationContext, MainActivity::class.java) + .setFlags(intentFlags) + .putExtra(IntentData.channelId, group.toID()) - val summaryId = ++notificationId + val pendingIntent = PendingIntentCompat + .getActivity(applicationContext, summaryId, intent, FLAG_UPDATE_CURRENT, false) + + // Create summary notification containing new streams for Android versions below 7.0. + val newStreams = applicationContext.resources + .getQuantityString(R.plurals.channel_new_streams, streams.size, streams.size) + val summary = NotificationCompat.InboxStyle() + streams.forEach { + summary.addLine(it.title) + } + val summaryNotification = createNotificationBuilder(group) + .setContentTitle(streams[0].uploaderName) + .setContentText(newStreams) + // The intent that will fire when the user taps the notification + .setContentIntent(pendingIntent) + .setGroupSummary(true) + .setStyle(summary) + .build() + + notificationManager.notify(summaryId, summaryNotification) + } + } + + private suspend fun showStreamNotification(group: String, stream: StreamItem) { + val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or + Intent.FLAG_ACTIVITY_CLEAR_TASK val intent = Intent(applicationContext, MainActivity::class.java) .setFlags(intentFlags) - .putExtra(IntentData.channelId, group.toID()) - + .putExtra(IntentData.videoId, stream.url!!.toID()) + val code = ++notificationId val pendingIntent = PendingIntentCompat - .getActivity(applicationContext, summaryId, intent, FLAG_UPDATE_CURRENT, false) + .getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false) - // Create summary notification containing new streams for Android versions below 7.0. - val newStreams = applicationContext.resources - .getQuantityString(R.plurals.channel_new_streams, streams.size, streams.size) - val summary = NotificationCompat.InboxStyle() - streams.forEach { - summary.addLine(it.title) - } - val summaryNotification = createNotificationBuilder(group) - .setContentTitle(streams[0].uploaderName) - .setContentText(newStreams) + val notificationBuilder = createNotificationBuilder(group) + .setContentTitle(stream.title) + .setContentText(stream.uploaderName) // The intent that will fire when the user taps the notification .setContentIntent(pendingIntent) - .setGroupSummary(true) - .setStyle(summary) - .build() - notificationManager.notify(summaryId, summaryNotification) + // Load stream thumbnails if the relevant toggle is enabled. + if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) { + val thumbnail = withContext(Dispatchers.IO) { + ImageHelper.getImage(applicationContext, stream.thumbnail).drawable?.toBitmap() + } + + notificationBuilder + .setLargeIcon(thumbnail) + .setStyle( + NotificationCompat.BigPictureStyle() + .bigPicture(thumbnail) + .bigLargeIcon(null as Bitmap?) // Hides the icon when expanding + ) + } + + notificationManager.notify(code, notificationBuilder.build()) } private fun createNotificationBuilder(group: String): NotificationCompat.Builder { From 2bb22cfbae3be5b3a5c9c9980ee3cd65566da5a5 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 14 Apr 2023 16:16:28 +0530 Subject: [PATCH 2/2] Configure only summary or single stream notifications to make noise. --- .../libretube/workers/NotificationWorker.kt | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index 4df75acff..a8eb6c6e1 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -139,17 +139,15 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : private suspend fun createNotificationsForChannel(group: String, streams: List) { // Create stream notifications. These are automatically grouped on Android 7.0 and later. if (streams.size == 1) { - showStreamNotification(group, streams[0]) + showStreamNotification(group, streams[0], true) } else { streams.forEach { - showStreamNotification(group, it) + showStreamNotification(group, it, false) } val summaryId = ++notificationId - val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or - Intent.FLAG_ACTIVITY_CLEAR_TASK val intent = Intent(applicationContext, MainActivity::class.java) - .setFlags(intentFlags) + .setFlags(INTENT_FLAGS) .putExtra(IntentData.channelId, group.toID()) val pendingIntent = PendingIntentCompat @@ -169,18 +167,20 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : .setContentIntent(pendingIntent) .setGroupSummary(true) .setStyle(summary) + .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) .build() notificationManager.notify(summaryId, summaryNotification) } } - private suspend fun showStreamNotification(group: String, stream: StreamItem) { - val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or - Intent.FLAG_ACTIVITY_CLEAR_TASK - + private suspend fun showStreamNotification( + group: String, + stream: StreamItem, + isSingleNotification: Boolean + ) { val intent = Intent(applicationContext, MainActivity::class.java) - .setFlags(intentFlags) + .setFlags(INTENT_FLAGS) .putExtra(IntentData.videoId, stream.url!!.toID()) val code = ++notificationId val pendingIntent = PendingIntentCompat @@ -191,6 +191,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : .setContentText(stream.uploaderName) // The intent that will fire when the user taps the notification .setContentIntent(pendingIntent) + .setSilent(!isSingleNotification) // Load stream thumbnails if the relevant toggle is enabled. if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) { @@ -217,4 +218,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : .setAutoCancel(true) .setGroup(group) } + + companion object { + private const val INTENT_FLAGS = Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } }