diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java --- a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java +++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java @@ -1,5 +1,6 @@ package app.comm.android.notifications; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.os.Bundle; @@ -12,6 +13,8 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; import com.google.firebase.messaging.RemoteMessage; +import java.util.HashMap; +import java.util.Map; public class CommAndroidNotifications extends ReactContextBaseJavaModule { private NotificationManager notificationManager; @@ -63,4 +66,31 @@ initialNotification); promise.resolve(jsReadableNotification); } + + @ReactMethod + public void createChannel( + String channelID, + String name, + int importance, + String description) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) { + // Method used below appeared in API level 26. Details: + // https://developer.android.com/develop/ui/views/notifications/channels#CreateChannel + return; + } + NotificationChannel channel = + new NotificationChannel(channelID, name, importance); + if (description != null) { + channel.setDescription(description); + } + notificationManager.createNotificationChannel(channel); + } + + @Override + public Map getConstants() { + final Map constants = new HashMap<>(); + constants.put( + "NOTIFICATIONS_IMPORTANCE_HIGH", NotificationManager.IMPORTANCE_HIGH); + return constants; + } } diff --git a/native/push/android.js b/native/push/android.js --- a/native/push/android.js +++ b/native/push/android.js @@ -7,6 +7,13 @@ type CommAndroidNotificationsModuleType = { +removeAllActiveNotificationsForThread: (threadID: string) => void, +getInitialNotification: () => Promise, + +createChannel: ( + channelID: string, + name: string, + importance: number, + description: ?string, + ) => void, + +getConstants: () => { +NOTIFICATIONS_IMPORTANCE_HIGH: number, ... }, ... }; export type AndroidForegroundMessage = { diff --git a/native/push/push-handler.react.js b/native/push/push-handler.react.js --- a/native/push/push-handler.react.js +++ b/native/push/push-handler.react.js @@ -148,13 +148,12 @@ ), ); } else if (Platform.OS === 'android') { - const firebase = getFirebase(); - const channel = new firebase.notifications.Android.Channel( + CommAndroidNotifications.createChannel( androidNotificationChannelID, 'Default', - firebase.notifications.Android.Importance.Max, - ).setDescription('Comm notifications channel'); - firebase.notifications().android.createChannel(channel); + CommAndroidNotifications.getConstants().NOTIFICATIONS_IMPORTANCE_HIGH, + 'Comm notifications channel', + ); const commAndroidNotificationsEventEmitter = getCommAndroidNotificationsEventEmitter(); this.androidNotificationsEventSubscriptions.push( commAndroidNotificationsEventEmitter.addListener(