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; @@ -12,6 +13,7 @@ import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.LifecycleOwner; 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; @@ -67,6 +69,7 @@ private static final long[] VIBRATION_SPEC = {500, 500}; private Bitmap displayableNotificationLargeIcon; private NotificationManager notificationManager; + private LocalBroadcastManager localBroadcastManager; private boolean isAppInForeground = false; public static final String TOKEN_EVENT = "TOKEN_EVENT"; @@ -78,6 +81,7 @@ 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); @@ -95,6 +99,13 @@ }); } + @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'; @@ -10,6 +10,7 @@ +removeAllActiveNotificationsForThread: (threadID: string) => void, }; +const { CommAndroidNotificationsEventEmitter } = NativeModules; const CommAndroidNotifications: CommAndroidNotificationsModuleType = NativeModules.CommAndroidNotifications; const androidNotificationChannelID = 'default'; @@ -42,9 +43,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 @@ -50,6 +50,7 @@ import { androidNotificationChannelID, handleAndroidMessage, + getCommAndroidNotificationsEventEmitter, CommAndroidNotifications, } from './android'; import { getFirebase } from './firebase'; @@ -106,7 +107,7 @@ }; currentState: ?string = getCurrentLifecycleState(); appStarted = 0; - androidTokenListener: ?() => void = null; + androidNotificationsEventSubscriptions: Array = []; androidMessageListener: ?() => void = null; androidNotifOpenListener: ?() => void = null; initialAndroidNotifHandled = false; @@ -144,9 +145,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 @@ this.iosNotificationOpened, ); } 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;