From 6e4a461c3c82646333828f781869255f1bbe056a Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Fri, 28 Jun 2024 22:42:04 +0200 Subject: [PATCH] Update summary notification when notification is dismissed Closes #3591 Signed-off-by: mueller-ma --- .../habdroid/core/NotificationPoller.kt | 4 +- .../core/FcmMessageListenerService.kt | 6 +-- mobile/src/main/AndroidManifest.xml | 3 ++ .../core/NotificationDismissedReceiver.kt | 36 +++++++++++++ .../habdroid/core/NotificationHelper.kt | 50 ++++++++++++------- 5 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 mobile/src/main/java/org/openhab/habdroid/core/NotificationDismissedReceiver.kt diff --git a/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt b/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt index 3e8bb5c711c..20e182598ad 100644 --- a/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt +++ b/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt @@ -67,9 +67,7 @@ object NotificationPoller { newMessages.forEach { message -> notifHelper.showNotification( - message, - null, - null + message ) } } diff --git a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt index b2e2b544b5c..69a522da46a 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt @@ -54,11 +54,7 @@ class FcmMessageListenerService : FirebaseMessagingService() { ) runBlocking { - notifHelper.showNotification( - cloudNotification, - null, - null - ) + notifHelper.showNotification(cloudNotification) } } "hideNotification" -> { diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml index 1b50979b88f..7aa02205c69 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -276,6 +276,9 @@ + 1) { - notificationManager.notify( - SUMMARY_NOTIFICATION_ID, - makeSummaryNotification(count, message.createdTimestamp, summaryDeleteIntent) - ) - } - } + updateGroupNotification() } fun cancelNotification(notificationId: Int) { @@ -79,10 +64,36 @@ class NotificationHelper(private val context: Context) { for (n in active) { notificationManager.cancel(n.id) } + } else { + updateGroupNotification() } } } + fun updateGroupNotification() { + if (!HAS_GROUPING_SUPPORT) { + return + } + val count = countCloudNotifications(notificationManager.activeNotifications) + if (count > 1) { + notificationManager.notify( + SUMMARY_NOTIFICATION_ID, + makeSummaryNotification(count, System.currentTimeMillis(), createDeleteIntent(SUMMARY_NOTIFICATION_ID)) + ) + } + } + + private fun createDeleteIntent(notificationId: Int): PendingIntent { + val intent = Intent(context, NotificationDismissedReceiver::class.java) + intent.putExtra(NOTIFICATION_ID_EXTRA, notificationId) + return PendingIntent.getBroadcast( + context, + notificationId, + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable + ) + } + private fun createChannelForSeverity(severity: String?) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { return @@ -185,7 +196,7 @@ class NotificationHelper(private val context: Context) { fun makeSummaryNotification( subNotificationCount: Int, timestamp: Long, - deleteIntent: PendingIntent? + deleteIntent: PendingIntent ): Notification { val text = context.resources.getQuantityString( R.plurals.summary_notification_text, @@ -240,6 +251,7 @@ class NotificationHelper(private val context: Context) { companion object { private val TAG = NotificationHelper::class.java.simpleName + const val NOTIFICATION_ID_EXTRA = "notification_id" private fun getChannelId(severity: String?) = if (severity.isNullOrEmpty()) { NotificationUpdateObserver.CHANNEL_ID_MESSAGE_DEFAULT