From 550756f2acab4b5f5872a24354528f6a82acd625 Mon Sep 17 00:00:00 2001 From: Jules Rosser Date: Fri, 16 Dec 2022 16:42:19 +0000 Subject: [PATCH 1/3] introduce Android notification channel --- .../CustomNotificationProvider.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java index deeff81bf76d..aa03e26a9931 100644 --- a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java +++ b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java @@ -1,5 +1,7 @@ package com.expensify.chat.customairshipextender; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -8,11 +10,13 @@ import android.graphics.Bitmap.Config; import android.graphics.PorterDuffXfermode; import android.graphics.PorterDuff.Mode; +import android.os.Build; import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; import android.view.WindowManager; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.app.Person; @@ -48,6 +52,9 @@ public class CustomNotificationProvider extends ReactNotificationProvider { // Logging private static final String TAG = "NotificationProvider"; + // Define notification channel + public static final String CHANNEL_MESSAGES_ID = "CHANNEL_MESSAGES"; + public static final String CHANNEL_MESSAGES_NAME = "Message Notifications"; // Conversation JSON keys private static final String PAYLOAD_KEY = "payload"; private static final String TYPE_KEY = "type"; @@ -58,6 +65,9 @@ public class CustomNotificationProvider extends ReactNotificationProvider { public CustomNotificationProvider(@NonNull Context context, @NonNull AirshipConfigOptions configOptions) { super(context, configOptions); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createAndRegisterNotificationChannel(context); + } } @NonNull @@ -82,6 +92,14 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @ return builder; } + @RequiresApi(api = Build.VERSION_CODES.O) + private void createAndRegisterNotificationChannel(@NonNull Context context) { + NotificationChannel channel = new NotificationChannel(CHANNEL_MESSAGES_ID, CHANNEL_MESSAGES_NAME, NotificationManager.IMPORTANCE_HIGH); + + NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + /** * Creates a canvas to draw a circle and then draws the bitmap avatar within that circle * to clip off the area of the bitmap outside the circular path and returns a circular From 58932f65da3be6948825df4cb5e487f220f1f566 Mon Sep 17 00:00:00 2001 From: Jules Rosser Date: Fri, 16 Dec 2022 16:43:12 +0000 Subject: [PATCH 2/3] increase Android notification priority, show above other apps --- .../CustomNotificationProvider.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java index aa03e26a9931..3dc4e63814d6 100644 --- a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java +++ b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java @@ -1,5 +1,7 @@ package com.expensify.chat.customairshipextender; +import static androidx.core.app.NotificationCompat.PRIORITY_MAX; + import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; @@ -76,6 +78,13 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @ super.onExtendBuilder(context, builder, arguments); PushMessage message = arguments.getMessage(); + // Configure the notification channel or priority to ensure it shows in foreground + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setChannelId(CHANNEL_MESSAGES_ID); + } else { + builder.setPriority(PRIORITY_MAX); + } + if (message.containsKey(PAYLOAD_KEY)) { try { JsonMap payload = JsonValue.parseString(message.getExtra(PAYLOAD_KEY)).optMap(); From fb01fc4e71b0876d50368b42b21d85e9942b2d01 Mon Sep 17 00:00:00 2001 From: Jules Rosser Date: Fri, 16 Dec 2022 16:43:37 +0000 Subject: [PATCH 3/3] introduce Android notification channel grouping --- .../customairshipextender/CustomNotificationProvider.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java index 3dc4e63814d6..7c1f4a245cd2 100644 --- a/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java +++ b/android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java @@ -3,6 +3,7 @@ import static androidx.core.app.NotificationCompat.PRIORITY_MAX; import android.app.NotificationChannel; +import android.app.NotificationChannelGroup; import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; @@ -57,6 +58,9 @@ public class CustomNotificationProvider extends ReactNotificationProvider { // Define notification channel public static final String CHANNEL_MESSAGES_ID = "CHANNEL_MESSAGES"; public static final String CHANNEL_MESSAGES_NAME = "Message Notifications"; + public static final String CHANNEL_GROUP_ID = "CHANNEL_GROUP_CHATS"; + public static final String CHANNEL_GROUP_NAME = "Chats"; + // Conversation JSON keys private static final String PAYLOAD_KEY = "payload"; private static final String TYPE_KEY = "type"; @@ -103,9 +107,12 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @ @RequiresApi(api = Build.VERSION_CODES.O) private void createAndRegisterNotificationChannel(@NonNull Context context) { + NotificationChannelGroup channelGroup = new NotificationChannelGroup(CHANNEL_GROUP_ID, CHANNEL_GROUP_NAME); NotificationChannel channel = new NotificationChannel(CHANNEL_MESSAGES_ID, CHANNEL_MESSAGES_NAME, NotificationManager.IMPORTANCE_HIGH); + channel.setGroup(CHANNEL_GROUP_ID); NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + notificationManager.createNotificationChannelGroup(channelGroup); notificationManager.createNotificationChannel(channel); }