Skip to content

Commit

Permalink
Fix up notification cancel behavior
Browse files Browse the repository at this point in the history
NotificationHelper.cancelNotification() was never called for the summary
notification, so the code handling it was misplaced there.

Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and mueller-ma committed Jul 11, 2024
1 parent d37b6f2 commit e946543
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class FcmMessageListenerService : FirebaseMessagingService() {
"hideNotification" -> {
data["tag"]?.let { tag -> notifHelper.cancelNotificationsByTag(tag) }
val id = CloudNotificationId(data["persistedId"].orEmpty(), data["reference-id"])
notifHelper.cancelNotification(id.notificationId)
notifHelper.cancelNotificationById(id)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NotificationHandlingReceiver : BroadcastReceiver() {
when (intent.action) {
ACTION_DISMISSED -> {
Log.d(TAG, "Dismissed notification $notificationId")
NotificationHelper(context).updateGroupNotification()
NotificationHelper(context).handleNotificationDismissed(notificationId)
}
ACTION_NOTIF_ACTION -> {
val cna = IntentCompat.getParcelableExtra(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,13 @@ class NotificationHelper(private val context: Context) {
updateGroupNotification()
}

fun cancelNotification(notificationId: Int) {
notificationManager.cancel(notificationId)
fun cancelNotificationById(id: CloudNotificationId) {
notificationManager.cancel(id.notificationId)
if (HAS_GROUPING_SUPPORT) {
val active = notificationManager.activeNotifications
if (notificationId != SUMMARY_NOTIFICATION_ID && countCloudNotifications(active) == 0) {
if (countCloudNotifications(active) == 0) {
// Cancel summary when removing the last sub-notification
notificationManager.cancel(SUMMARY_NOTIFICATION_ID)
} else if (notificationId == SUMMARY_NOTIFICATION_ID) {
// Cancel all sub-notifications when removing the summary
for (n in active) {
notificationManager.cancel(n.id)
}
} else {
updateGroupNotification()
}
Expand All @@ -80,7 +75,19 @@ class NotificationHelper(private val context: Context) {
.forEach { sbn -> notificationManager.cancel(sbn.id) }
}

fun updateGroupNotification() {
fun handleNotificationDismissed(notificationId: Int) {
if (!HAS_GROUPING_SUPPORT) {
return
}
if (notificationId == SUMMARY_NOTIFICATION_ID) {
// Cancel all sub-notifications when removing the summary
notificationManager.activeNotifications.forEach { notificationManager.cancel(it.id) }
} else {
updateGroupNotification()
}
}

private fun updateGroupNotification() {
if (!HAS_GROUPING_SUPPORT) {
return
}
Expand Down

0 comments on commit e946543

Please sign in to comment.