fix(android): Fix notification grouping with the foreground notification

This commit is contained in:
PapaTutuWawa 2023-08-28 13:36:36 +02:00
parent 11ea1fae12
commit 27440067cd
4 changed files with 47 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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"

View File

@ -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())
}

View File

@ -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