Skip to content

create default channel only when no existing channel #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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