diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationFetcher.kt b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationFetcher.kt index deaa6d4b5..d735af3c1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationFetcher.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationFetcher.kt @@ -90,23 +90,29 @@ class NotificationFetcher @Inject constructor( } } + val notificationsByType = notifications.groupBy { it.type } + // Make and send the new notifications // TODO: Use the batch notification API available in NotificationManagerCompat // 1.11 and up (https://developer.android.com/jetpack/androidx/releases/core#1.11.0-alpha01) // when it is released. - notifications.forEachIndexed { index, notification -> - val androidNotification = NotificationHelper.make( - context, - notificationManager, - notification, - account, - index == 0 - ) - notificationManager.notify(notification.id, account.id.toInt(), androidNotification) - // Android will rate limit / drop notifications if they're posted too - // quickly. There is no indication to the user that this happened. - // See https://github.com/tuskyapp/Tusky/pull/3626#discussion_r1192963664 - delay(1000.milliseconds) + + notificationsByType.forEach { notificationsGroup -> + notificationsGroup.value.forEach { notification -> + val androidNotification = NotificationHelper.make( + context, + notificationManager, + notification, + account, + notificationsGroup.value.size == 1 + ) + notificationManager.notify(notification.id, account.id.toInt(), androidNotification) + + // Android will rate limit / drop notifications if they're posted too + // quickly. There is no indication to the user that this happened. + // See https://github.com/tuskyapp/Tusky/pull/3626#discussion_r1192963664 + delay(1000.milliseconds) + } } NotificationHelper.updateSummaryNotifications( diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java index 3479f86fb..14012c6e8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java @@ -149,7 +149,7 @@ public class NotificationHelper { * @return the new notification */ @NonNull - public static android.app.Notification make(final @NonNull Context context, @NonNull NotificationManager notificationManager, @NonNull Notification body, @NonNull AccountEntity account, boolean isFirstOfBatch) { + public static android.app.Notification make(final @NonNull Context context, @NonNull NotificationManager notificationManager, @NonNull Notification body, @NonNull AccountEntity account, boolean isOnlyOneInGroup) { body = body.rewriteToStatusTypeIfNeeded(account.getAccountId()); String mastodonNotificationId = body.getId(); int accountId = (int) account.getId(); @@ -241,7 +241,7 @@ public class NotificationHelper { builder.addExtras(extras); // Only alert for the first notification of a batch to avoid multiple alerts at once - if(!isFirstOfBatch) { + if(!isOnlyOneInGroup) { builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); }