Skip to content

Commit e5dad8a

Browse files
authored
create default channel only when no existing channel (#839)
1 parent 22f4f44 commit e5dad8a

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,15 @@ private int getAppResourceId(String resName, String resType) {
225225

226226
private void initDefaultChannel(Context context) {
227227
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
228-
NotificationChannel defaultChannel = new NotificationChannel(DEFAULT_CHANNEL_ID,
229-
DEFAULT_CHANNEL_NAME,
230-
NotificationManager.IMPORTANCE_DEFAULT);
231228
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
232-
notificationManager.createNotificationChannel(defaultChannel);
229+
if (notificationManager.getNotificationChannels().size() == 0) {
230+
NotificationChannel defaultChannel = new NotificationChannel(
231+
DEFAULT_CHANNEL_ID,
232+
DEFAULT_CHANNEL_NAME,
233+
NotificationManager.IMPORTANCE_DEFAULT
234+
);
235+
notificationManager.createNotificationChannel(defaultChannel);
236+
}
233237
}
234238
}
235239
}

lib/android/app/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.app.Notification;
5+
import android.app.NotificationChannel;
56
import android.app.NotificationManager;
67
import android.content.Context;
78
import android.content.Intent;
@@ -32,7 +33,6 @@
3233
import org.robolectric.Shadows;
3334
import org.robolectric.shadows.ShadowNotification;
3435

35-
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_BACKGROUND_EVENT_NAME;
3636
import static org.junit.Assert.assertEquals;
3737
import static org.junit.Assert.assertNotEquals;
3838
import static org.mockito.ArgumentMatchers.any;
@@ -45,6 +45,11 @@
4545
import static org.mockito.Mockito.when;
4646
import static org.mockito.Mockito.argThat;
4747

48+
import java.util.ArrayList;
49+
import java.util.List;
50+
51+
import edu.emory.mathcs.backport.java.util.Arrays;
52+
4853
@RunWith(RobolectricTestRunner.class)
4954
public class PushNotificationTest {
5055

@@ -304,6 +309,24 @@ public void onPostRequest_emptyData_postNotification() throws Exception {
304309
verify(mNotificationManager, never()).notify(anyInt(), any(Notification.class));
305310
}
306311

312+
@Test
313+
public void onCreate_noExistingChannel_createDefaultChannel() throws Exception {
314+
createUUT();
315+
316+
verify(mNotificationManager).createNotificationChannel(any(NotificationChannel.class));
317+
}
318+
319+
@Test
320+
public void onCreate_existingChannel_notCreateDefaultChannel() throws Exception {
321+
List<NotificationChannel> existingChannel = new ArrayList<>();
322+
existingChannel.add(new NotificationChannel("id", "name", 1));
323+
when(mNotificationManager.getNotificationChannels()).thenReturn(existingChannel);
324+
325+
createUUT();
326+
327+
verify(mNotificationManager, never()).createNotificationChannel(any(NotificationChannel.class));
328+
}
329+
307330
protected PushNotification createUUT() {
308331
return createUUT(mNotificationBundle);
309332
}

0 commit comments

Comments
 (0)