From 27440067cdad627c7496be2baa75be586b9cba1e Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Mon, 28 Aug 2023 13:36:36 +0200 Subject: [PATCH] fix(android): Fix notification grouping with the foreground notification --- .../BackgroundService.java | 44 ++++++++++++++++--- .../polynom/moxplatform_android/Constants.kt | 4 ++ .../NotificationReceiver.kt | 1 + .../moxplatform_android/Notifications.kt | 5 +++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java index d4a7df0..79a6af7 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/BackgroundService.java @@ -1,9 +1,13 @@ package me.polynom.moxplatform_android; +import static me.polynom.moxplatform_android.ConstantsKt.GROUP_KEY_FOREGROUND; +import static me.polynom.moxplatform_android.ConstantsKt.GROUP_KEY_MESSAGES; +import static me.polynom.moxplatform_android.ConstantsKt.GROUP_KEY_OTHER; import static me.polynom.moxplatform_android.ConstantsKt.SHARED_PREFERENCES_KEY; import android.app.AlarmManager; import android.app.NotificationChannel; +import android.app.NotificationChannelGroup; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -98,14 +102,40 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - CharSequence name = "Moxxy Background Service"; - String description = "Executing Moxxy in the background"; - - int importance = NotificationManager.IMPORTANCE_LOW; - NotificationChannel channel = new NotificationChannel("FOREGROUND_DEFAULT", name, importance); - channel.setDescription(description); - NotificationManager notificationManager = getSystemService(NotificationManager.class); + + // Create a special notification group for the foreground service notification to prevent + // message notifications from getting grouped with it in >= Android 13. + notificationManager.createNotificationChannelGroup( + new NotificationChannelGroup( + GROUP_KEY_FOREGROUND, + "The foreground notification" + ) + ); + notificationManager.createNotificationChannelGroup( + new NotificationChannelGroup( + GROUP_KEY_MESSAGES, + "Messages" + ) + ); + notificationManager.createNotificationChannelGroup( + new NotificationChannelGroup( + GROUP_KEY_OTHER, + "Other" + ) + ); + + NotificationChannel channel = new NotificationChannel( + "FOREGROUND_DEFAULT", + "Moxxy Background Service", + NotificationManager.IMPORTANCE_LOW + ); + channel.setDescription("Executing Moxxy in the background"); + // Prevent showing a badge in the Launcher + channel.setShowBadge(false); + channel.setGroup("foreground"); + + // Create the channel notificationManager.createNotificationChannel(channel); } } diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt index edd0eef..ea5710f 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Constants.kt @@ -6,6 +6,10 @@ const val TAG = "Moxplatform" // The size of the buffer to hashing, encryption, and decryption in bytes. const val BUFFER_SIZE = 8096 +const val GROUP_KEY_FOREGROUND = "foreground" +const val GROUP_KEY_MESSAGES = "messages" +const val GROUP_KEY_OTHER = "other" + // The data key for text entered in the notification's reply field const val REPLY_TEXT_KEY = "key_reply_text" diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt index fe7ea96..16d88a0 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/NotificationReceiver.kt @@ -161,6 +161,7 @@ class NotificationReceiver : BroadcastReceiver() { val recoveredBuilder = Notification.Builder.recoverBuilder(context, notification).apply { style = newStyle setOnlyAlertOnce(true) + setGroup(GROUP_KEY_MESSAGES) } NotificationManagerCompat.from(context).notify(id, recoveredBuilder.build()) } diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt index 0d76c78..32d124d 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Notifications.kt @@ -254,6 +254,9 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif setContentTitle(notification.title) } + // Prevent grouping with the foreground service + setGroup(GROUP_KEY_MESSAGES) + setAllowSystemGeneratedContextualActions(true) setCategory(Notification.CATEGORY_MESSAGE) @@ -278,6 +281,8 @@ fun showNotification(context: Context, notification: Api.RegularNotification) { Api.NotificationIcon.WARNING -> setSmallIcon(R.drawable.warning) Api.NotificationIcon.NONE -> {} } + + setGroup(GROUP_KEY_OTHER) }.build() // Post the notification