diff --git a/.spellcheck.dict.txt b/.spellcheck.dict.txt index 0470b7d3ac..d76ffd1beb 100644 --- a/.spellcheck.dict.txt +++ b/.spellcheck.dict.txt @@ -123,3 +123,4 @@ v6 VSCode Wix Xcode +lifecycle diff --git a/docs/messaging/notifications.md b/docs/messaging/notifications.md index 15e002b0c6..9e9137bbf4 100644 --- a/docs/messaging/notifications.md +++ b/docs/messaging/notifications.md @@ -164,6 +164,9 @@ function App() { } ``` +The call to `getInitialNotification` should happen within a React lifecycle method after mounting (e.g. `componentDidMount` or `useEffect`). +If it's called too soon (e.g. within a class constructor or global scope), the notification data may not be available. + # Notifee - Advanced Notifications FCM provides support for displaying basic notifications to users with minimal integration required. If however you require 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 06b62c1211..64b86ceb26 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 @@ -19,6 +19,7 @@ import android.app.Activity; import android.content.Intent; +import android.util.Log; import androidx.core.app.NotificationManagerCompat; @@ -55,23 +56,29 @@ public void getInitialNotification(Promise promise) { initialNotification = null; return; } else { - Intent intent = getCurrentActivity().getIntent(); + Activity activity = getCurrentActivity(); - if (intent != null && intent.getExtras() != null) { - // messageId can be either one... - String messageId = intent.getExtras().getString("google.message_id"); - if (messageId == null) messageId = intent.getExtras().getString("message_id"); + if (activity != null) { + Intent intent = activity.getIntent(); - // only handle non-consumed initial notifications - if (messageId != null && initialNotificationMap.get(messageId) == null) { - RemoteMessage remoteMessage = ReactNativeFirebaseMessagingReceiver.notifications.get(messageId); + if (intent != null && intent.getExtras() != null) { + // messageId can be either one... + String messageId = intent.getExtras().getString("google.message_id"); + if (messageId == null) messageId = intent.getExtras().getString("message_id"); - if (remoteMessage != null) { - promise.resolve(ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage)); - initialNotificationMap.put(messageId, true); - return; + // only handle non-consumed initial notifications + if (messageId != null && initialNotificationMap.get(messageId) == null) { + RemoteMessage remoteMessage = ReactNativeFirebaseMessagingReceiver.notifications.get(messageId); + + if (remoteMessage != null) { + promise.resolve(ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap(remoteMessage)); + initialNotificationMap.put(messageId, true); + return; + } } } + } else { + Log.w(TAG, "Attempt to call getInitialNotification failed. The current activity is not ready, try calling the method later in the React lifecycle."); } }