From 6a30d4b4798c0ff9f0d5e406d4da5fb47e313069 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Thu, 29 Apr 2021 15:07:04 -0500 Subject: [PATCH] fix(messaging, android): repair crash handling remote notifications WritableNativeMap has a check to see if it is consumed or not when it is used, to prevent resolving a Promise in native code then attempting to modify it. We may use the same WritableNativeMap object twice though when handling deferred initial notifications. Crash repair is to store a copy so the Maps are distinct --- .../messaging/ReactNativeFirebaseMessagingModule.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingModule.java b/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingModule.java index 7196021b11..71565b08ca 100644 --- a/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingModule.java +++ b/packages/messaging/android/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingModule.java @@ -83,7 +83,7 @@ public void getInitialNotification(Promise promise) { } else { remoteMessageMap = ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage); } - if (remoteMessageMap != null){ + if (remoteMessageMap != null) { promise.resolve(remoteMessageMap); initialNotificationMap.put(messageId, true); return; @@ -228,9 +228,10 @@ public void onNewIntent(Intent intent) { } else { remoteMessageMap = ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage); } - - if (remoteMessageMap != null){ - initialNotification = remoteMessageMap; + + if (remoteMessageMap != null) { + // WritableNativeMap not be consumed twice. But it is resolved in future and in event below. Make a copy - issue #5231 + initialNotification = remoteMessageMap.copy(); ReactNativeFirebaseMessagingReceiver.notifications.remove(messageId); ReactNativeFirebaseEventEmitter emitter = ReactNativeFirebaseEventEmitter.getSharedInstance();