Skip to content

Commit

Permalink
fix(messaging): Missing notification on restart (#5181)
Browse files Browse the repository at this point in the history
* fix(messaging, android): populate RemoteMessage.notification for getInitialNotification

The notification store that makes sure the getInitialNotification method returns valid information across app restarts was persisting the RemoteMessage correctly but not returning the notification object from that RemoteMessage, this populates the notification object correctly
  • Loading branch information
mcorner committed Apr 19, 2021
1 parent 2c6a6a8 commit ea6e138
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
Expand All @@ -41,25 +42,25 @@

public class ReactNativeFirebaseMessagingModule extends ReactNativeFirebaseModule implements ActivityEventListener {
private static final String TAG = "Messaging";
RemoteMessage initialNotification = null;
ReadableMap initialNotification = null;
private HashMap<String, Boolean> initialNotificationMap = new HashMap<>();

ReactNativeFirebaseMessagingModule(ReactApplicationContext reactContext) {
super(reactContext, TAG);
reactContext.addActivityEventListener(this);
}

private RemoteMessage popRemoteMessageFromMessagingStore(String messageId) {
private WritableMap popRemoteMessageMapFromMessagingStore(String messageId) {
ReactNativeFirebaseMessagingStore messagingStore = ReactNativeFirebaseMessagingStoreHelper.getInstance().getMessagingStore();
RemoteMessage remoteMessage = messagingStore.getFirebaseMessage(messageId);
WritableMap remoteMessageMap = messagingStore.getFirebaseMessageMap(messageId);
messagingStore.clearFirebaseMessage(messageId);
return remoteMessage;
return remoteMessageMap;
}

@ReactMethod
public void getInitialNotification(Promise promise) {
if (initialNotification != null) {
promise.resolve(ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(initialNotification));
promise.resolve(initialNotification);
initialNotification = null;
return;
} else {
Expand All @@ -75,12 +76,15 @@ public void getInitialNotification(Promise promise) {

// only handle non-consumed initial notifications
if (messageId != null && initialNotificationMap.get(messageId) == null) {
WritableMap remoteMessageMap;
RemoteMessage remoteMessage = ReactNativeFirebaseMessagingReceiver.notifications.get(messageId);
if (remoteMessage == null) {
remoteMessage = popRemoteMessageFromMessagingStore(messageId);
remoteMessageMap = popRemoteMessageMapFromMessagingStore(messageId);
} else {
remoteMessageMap = ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage);
}
if (remoteMessage != null) {
promise.resolve(ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage));
if (remoteMessageMap != null){
promise.resolve(remoteMessageMap);
initialNotificationMap.put(messageId, true);
return;
}
Expand Down Expand Up @@ -217,16 +221,20 @@ public void onNewIntent(Intent intent) {

if (messageId != null) {
RemoteMessage remoteMessage = ReactNativeFirebaseMessagingReceiver.notifications.get(messageId);
WritableMap remoteMessageMap;

if (remoteMessage == null) {
remoteMessage = popRemoteMessageFromMessagingStore(messageId);
remoteMessageMap = popRemoteMessageMapFromMessagingStore(messageId);
} else {
remoteMessageMap = ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage);
}

if (remoteMessage != null) {
initialNotification = remoteMessage;
if (remoteMessageMap != null){
initialNotification = remoteMessageMap;
ReactNativeFirebaseMessagingReceiver.notifications.remove(messageId);

ReactNativeFirebaseEventEmitter emitter = ReactNativeFirebaseEventEmitter.getSharedInstance();
emitter.sendEvent(ReactNativeFirebaseMessagingSerializer.remoteMessageToEvent(remoteMessage, true));
emitter.sendEvent(ReactNativeFirebaseMessagingSerializer.remoteMessageMapToEvent(remoteMessageMap, true));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public static ReactNativeFirebaseEvent remoteMessageToEvent(RemoteMessage remote
return new ReactNativeFirebaseEvent(openEvent ? EVENT_NOTIFICATION_OPENED : EVENT_MESSAGE_RECEIVED, remoteMessageToWritableMap(remoteMessage));
}

public static ReactNativeFirebaseEvent remoteMessageMapToEvent(WritableMap remoteMessageMap, Boolean openEvent) {
return new ReactNativeFirebaseEvent(openEvent ? EVENT_NOTIFICATION_OPENED : EVENT_MESSAGE_RECEIVED, remoteMessageMap);
}

public static ReactNativeFirebaseEvent newTokenToTokenEvent(String newToken) {
WritableMap eventBody = Arguments.createMap();
eventBody.putString(KEY_TOKEN, newToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import com.google.firebase.messaging.RemoteMessage;

import com.facebook.react.bridge.WritableMap;

public interface ReactNativeFirebaseMessagingStore {
void storeFirebaseMessage(RemoteMessage remoteMessage);

@Deprecated
RemoteMessage getFirebaseMessage(String remoteMessageId);

WritableMap getFirebaseMessageMap(String remoteMessageId);

void clearFirebaseMessage(String remoteMessageId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.invertase.firebase.messaging;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.google.firebase.messaging.RemoteMessage;

Expand Down Expand Up @@ -47,16 +48,26 @@ public void storeFirebaseMessage(RemoteMessage remoteMessage) {
}
}

@Deprecated
@Override
public RemoteMessage getFirebaseMessage(String remoteMessageId) {
ReadableMap messageMap = getFirebaseMessageMap(remoteMessageId);
if (messageMap != null){
return remoteMessageFromReadableMap(messageMap);
}
return null;
}

@Override
public WritableMap getFirebaseMessageMap(String remoteMessageId) {
String remoteMessageString = UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null);
if (remoteMessageString != null) {
// Log.d("getFirebaseMessage", remoteMessageString);
try {
WritableMap readableMap = jsonToReact(new JSONObject(remoteMessageString));
readableMap.putString("to", remoteMessageId);//fake to
return remoteMessageFromReadableMap(readableMap);
} catch (JSONException e) {
WritableMap remoteMessageMap = jsonToReact(new JSONObject(remoteMessageString));
remoteMessageMap.putString("to", remoteMessageId);//fake to
return remoteMessageMap;
} catch (JSONException e) {
e.printStackTrace();
}
}
Expand Down

1 comment on commit ea6e138

@vercel
Copy link

@vercel vercel bot commented on ea6e138 Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.