From e5dad8ad56080bf5f68d061a67ade8dc2f8ea857 Mon Sep 17 00:00:00 2001 From: DanielEliraz Date: Mon, 14 Mar 2022 14:52:01 +0200 Subject: [PATCH] create default channel only when no existing channel (#839) --- .../core/notification/PushNotification.java | 12 ++++++--- .../notification/PushNotificationTest.java | 25 ++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java index b93f762a4..54a5fb83c 100644 --- a/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +++ b/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java @@ -225,11 +225,15 @@ private int getAppResourceId(String resName, String resType) { private void initDefaultChannel(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel defaultChannel = new NotificationChannel(DEFAULT_CHANNEL_ID, - DEFAULT_CHANNEL_NAME, - NotificationManager.IMPORTANCE_DEFAULT); final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.createNotificationChannel(defaultChannel); + if (notificationManager.getNotificationChannels().size() == 0) { + NotificationChannel defaultChannel = new NotificationChannel( + DEFAULT_CHANNEL_ID, + DEFAULT_CHANNEL_NAME, + NotificationManager.IMPORTANCE_DEFAULT + ); + notificationManager.createNotificationChannel(defaultChannel); + } } } } diff --git a/lib/android/app/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java b/lib/android/app/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java index fb2d55e7d..d8fec928f 100644 --- a/lib/android/app/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java +++ b/lib/android/app/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; @@ -32,7 +33,6 @@ import org.robolectric.Shadows; import org.robolectric.shadows.ShadowNotification; -import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_BACKGROUND_EVENT_NAME; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.mockito.ArgumentMatchers.any; @@ -45,6 +45,11 @@ import static org.mockito.Mockito.when; import static org.mockito.Mockito.argThat; +import java.util.ArrayList; +import java.util.List; + +import edu.emory.mathcs.backport.java.util.Arrays; + @RunWith(RobolectricTestRunner.class) public class PushNotificationTest { @@ -304,6 +309,24 @@ public void onPostRequest_emptyData_postNotification() throws Exception { verify(mNotificationManager, never()).notify(anyInt(), any(Notification.class)); } + @Test + public void onCreate_noExistingChannel_createDefaultChannel() throws Exception { + createUUT(); + + verify(mNotificationManager).createNotificationChannel(any(NotificationChannel.class)); + } + + @Test + public void onCreate_existingChannel_notCreateDefaultChannel() throws Exception { + List existingChannel = new ArrayList<>(); + existingChannel.add(new NotificationChannel("id", "name", 1)); + when(mNotificationManager.getNotificationChannels()).thenReturn(existingChannel); + + createUUT(); + + verify(mNotificationManager, never()).createNotificationChannel(any(NotificationChannel.class)); + } + protected PushNotification createUUT() { return createUUT(mNotificationBundle); }