Skip to content

Commit

Permalink
create default channel only when no existing channel (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielEliraz authored Mar 14, 2022
1 parent 22f4f44 commit e5dad8a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<NotificationChannel> 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);
}
Expand Down

0 comments on commit e5dad8a

Please sign in to comment.