Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(messaging): Missing notification on restart #5181

Merged
merged 8 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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