Skip to content

Commit

Permalink
fix(messaging): add activity check to getInitialNotification (#3495)
Browse files Browse the repository at this point in the history
* fix(messaging): add activity check to getInitialNotification

* fix(messaging): add activity check to getInitialNotification

* Update .spellcheck.dict.txt

Co-authored-by: Mike Diarmid <[email protected]>
  • Loading branch information
Ehesp and Salakar authored Apr 22, 2020
1 parent fb70797 commit a8d2a2f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions .spellcheck.dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ v6
VSCode
Wix
Xcode
lifecycle
3 changes: 3 additions & 0 deletions docs/messaging/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import androidx.core.app.NotificationManagerCompat;

Expand Down Expand Up @@ -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.");
}
}

Expand Down

0 comments on commit a8d2a2f

Please sign in to comment.