-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[expo-notifications] Add NotificationChannelsManager
NotificationChannelsManager singleton module should be capable of providing modules with a fallback notification channel.
- Loading branch information
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...va/expo/modules/notifications/notifications/channels/ExpoNotificationChannelsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...java/expo/modules/notifications/notifications/interfaces/NotificationChannelsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/expo-notifications/android/src/main/res/values/strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |