diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java b/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java --- a/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java +++ b/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java @@ -3,6 +3,7 @@ import android.app.Notification; import android.app.NotificationManager; import android.content.Context; +import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; @@ -11,6 +12,7 @@ import androidx.core.app.NotificationCompat; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.ProcessLifecycleOwner; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import app.comm.android.ExpoUtils; import app.comm.android.R; import app.comm.android.fbjni.CommSecureStore; @@ -66,6 +68,7 @@ private static final long[] VIBRATION_SPEC = {500, 500}; private Bitmap displayableNotificationLargeIcon; private NotificationManager notificationManager; + private LocalBroadcastManager localBroadcastManager; public static final String TOKEN_EVENT = "TOKEN_EVENT"; @@ -76,10 +79,18 @@ ExpoUtils.createExpoSecureStoreSupplier(this.getApplicationContext())); notificationManager = (NotificationManager)this.getSystemService( Context.NOTIFICATION_SERVICE); + localBroadcastManager = LocalBroadcastManager.getInstance(this); displayableNotificationLargeIcon = BitmapFactory.decodeResource( this.getApplicationContext().getResources(), R.mipmap.ic_launcher); } + @Override + public void onNewToken(String token) { + Intent intent = new Intent(TOKEN_EVENT); + intent.putExtra("token", token); + localBroadcastManager.sendBroadcast(intent); + } + @Override public void onMessageReceived(RemoteMessage message) { String rescind = message.getData().get(RESCIND_KEY); diff --git a/native/push/android.js b/native/push/android.js --- a/native/push/android.js +++ b/native/push/android.js @@ -1,7 +1,7 @@ // @flow import invariant from 'invariant'; -import { NativeModules } from 'react-native'; +import { NativeModules, NativeEventEmitter } from 'react-native'; import type { RemoteMessage } from 'react-native-firebase'; import { mergePrefixIntoBody } from 'lib/shared/notif-utils'; @@ -11,6 +11,7 @@ ... }; +const { CommAndroidNotificationsEventEmitter } = NativeModules; const CommAndroidNotifications: CommAndroidNotificationsModuleType = NativeModules.CommAndroidNotifications; const androidNotificationChannelID = 'default'; @@ -43,9 +44,15 @@ } } } +function getCommAndroidNotificationsEventEmitter(): NativeEventEmitter<{ + commAndroidNotificationsToken: [string], +}> { + return new NativeEventEmitter(CommAndroidNotificationsEventEmitter); +} export { androidNotificationChannelID, handleAndroidMessage, + getCommAndroidNotificationsEventEmitter, CommAndroidNotifications, }; 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 @@ -49,6 +49,7 @@ import { androidNotificationChannelID, handleAndroidMessage, + getCommAndroidNotificationsEventEmitter, CommAndroidNotifications, } from './android'; import { @@ -113,7 +114,7 @@ }; currentState: ?string = getCurrentLifecycleState(); appStarted = 0; - androidTokenListener: ?() => void = null; + androidNotificationsEventSubscriptions: Array = []; androidMessageListener: ?() => void = null; androidNotifOpenListener: ?() => void = null; initialAndroidNotifHandled = false; @@ -156,9 +157,13 @@ firebase.notifications.Android.Importance.Max, ).setDescription('Comm notifications channel'); firebase.notifications().android.createChannel(channel); - this.androidTokenListener = firebase - .messaging() - .onTokenRefresh(this.handleAndroidDeviceToken); + const commAndroidNotificationsEventEmitter = getCommAndroidNotificationsEventEmitter(); + this.androidNotificationsEventSubscriptions.push( + commAndroidNotificationsEventEmitter.addListener( + 'commAndroidNotificationsToken', + this.handleAndroidDeviceToken, + ), + ); this.androidMessageListener = firebase .messaging() .onMessage(this.androidMessageReceived); @@ -182,10 +187,11 @@ iosNotificationEventSubscription.remove(); } } else if (Platform.OS === 'android') { - if (this.androidTokenListener) { - this.androidTokenListener(); - this.androidTokenListener = null; + for (const androidNotificationsEventSubscription of this + .androidNotificationsEventSubscriptions) { + androidNotificationsEventSubscription.remove(); } + this.androidNotificationsEventSubscriptions = []; if (this.androidMessageListener) { this.androidMessageListener(); this.androidMessageListener = null;