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 @@ -4,11 +4,13 @@ import android.app.Notification; import android.app.NotificationManager; import android.content.Context; +import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Bundle; import android.service.notification.StatusBarNotification; import android.util.Log; import androidx.core.app.NotificationCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import app.comm.android.ExpoUtils; import app.comm.android.R; import app.comm.android.fbjni.CommSecureStore; @@ -75,6 +77,13 @@ Context.NOTIFICATION_SERVICE); } + @Override + public void onNewToken(String token) { + Intent intent = new Intent(TOKEN_EVENT); + intent.putExtra("token", token); + LocalBroadcastManager.getInstance(this).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,12 +1,15 @@ // @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'; -const { CommAndroidNotifications } = NativeModules; +const { + CommAndroidNotifications, + CommAndroidNotificationsEventEmitter, +} = NativeModules; const androidNotificationChannelID = 'default'; @@ -38,9 +41,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;