Skip to content

Commit

Permalink
[expo-notifications] Add NotificationChannelsManager
Browse files Browse the repository at this point in the history
NotificationChannelsManager singleton module should be capable of providing modules with a fallback notification channel.
  • Loading branch information
sjchmiela committed Jan 24, 2020
1 parent f6d67bf commit 67ec888
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.unimodules.core.interfaces.SingletonModule;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import expo.modules.notifications.installationid.InstallationIdProvider;
import expo.modules.notifications.notifications.channels.ExpoNotificationChannelsManager;
import expo.modules.notifications.tokens.PushTokenManager;
import expo.modules.notifications.tokens.PushTokenModule;

Expand All @@ -25,6 +25,9 @@ public List<ExportedModule> createExportedModules(Context context) {

@Override
public List<SingletonModule> createSingletonModules(Context context) {
return Collections.singletonList((SingletonModule) new PushTokenManager());
return Arrays.asList(
new PushTokenManager(),
new ExpoNotificationChannelsManager(context)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package expo.modules.notifications.notifications.channels;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import org.unimodules.core.interfaces.SingletonModule;

import androidx.annotation.RequiresApi;
import expo.modules.notifications.R;
import expo.modules.notifications.notifications.interfaces.NotificationChannelsManager;

public class ExpoNotificationChannelsManager implements SingletonModule, NotificationChannelsManager {
private final static String SINGLETON_NAME = "NotificationChannelsManager";

private final static String FALLBACK_CHANNEL_ID = "expo_notifications_fallback_notification_channel";

@RequiresApi(api = Build.VERSION_CODES.N)
private final static int FALLBACK_CHANNEL_IMPORTANCE = NotificationManager.IMPORTANCE_HIGH;

private Context mContext;

public ExpoNotificationChannelsManager(Context context) {
mContext = context;
}

@Override
public String getName() {
return SINGLETON_NAME;
}

@Override
public NotificationChannel getFallbackNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return null;
}

NotificationChannel channel = getNotificationManager().getNotificationChannel(FALLBACK_CHANNEL_ID);
if (channel != null) {
return channel;
}

return createFallbackChannel();
}

@RequiresApi(api = Build.VERSION_CODES.O)
protected NotificationChannel createFallbackChannel() {
NotificationChannel channel = new NotificationChannel(FALLBACK_CHANNEL_ID, getFallbackChannelName(), FALLBACK_CHANNEL_IMPORTANCE);
channel.setShowBadge(true);
channel.enableVibration(true);
getNotificationManager().createNotificationChannel(channel);
return channel;
}

protected String getFallbackChannelName() {
return mContext.getString(R.string.expo_notifications_fallback_channel_name);
}

private NotificationManager getNotificationManager() {
return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package expo.modules.notifications.notifications.interfaces;

import android.app.NotificationChannel;

public interface NotificationChannelsManager {
NotificationChannel getFallbackNotificationChannel();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="expo_notifications_fallback_channel_name">Miscellaneous</string>
</resources>

0 comments on commit 67ec888

Please sign in to comment.