react-native-notifications: [v3][Android] Opening app through background notification doesn't trigger appropriate event
I’m using v3.1.1 of the library. When I receive background notification and tap on it, it opens my app, however Notifications.getInitialNotification() nor Notifications.events().registerNotificationReceivedBackground are triggered or have data.
Possibly related issue: https://github.com/wix/react-native-notifications/issues/445
Interestingly notifications that are received in foreground don’t have this issue. If I receive notification in foreground, close my app and then click on such notification I am seeing correct behaviour in the app.
Foreground notifications come with problem of their own described here (they are empty) https://github.com/wix/react-native-notifications/issues/456
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 22 (3 by maintainers)
Hi, I too had faced the same kind of issue so I am posting my findings here. First we need to understand when the notification is received and where it’s sent from as the behavior depends on both these aspects.
Now let’s mix and match these cases and see from where we can get the payload:
registerNotificationOpenedregisterNotificationOpenedgetInitialNotificationNow why does this occur: As per Handling Messages, the notifications that are generated using Admin SDK are received by this library code, whereas the notifications that are sent by the FCM console are received by the Firebase SDK and the notification is generated by it. Now in this case the data payload is delivered in the extras of the intent of your launcher Activity.
You can get this data from your launcher activity’s; This can be verified using: By adding this in your launcher activity; For the sake of simplicity the code considers a random timer of 10 seconds to allow the listener to be set otherwise the event will be emitted before the listener is attached, but you can get the data using some native method which gets called from the JS instead of this timer )
And by adding this in your JS where you want the notification
So this is kind of a hacky solution, but it works
Edit: If your sending the notification from console and the app is killed https://github.com/wix/react-native-notifications/issues/326#issuecomment-507371366 might work, worked for me;
Although it does not work in one case: App Killed -> Notification Received from the console -> App Opened via launcher -> Clicked on the notification This does not do anything as the OnCreate event has already passed.
Currently, I ended up having a “pushNotification” key against a blank value in the Firebase Console’s Additional Options, until I find a better way to do this. The purpose of the key is that the library takes some decisions based on this key and it just needs to to be present to satisfy a condition which was failing
I also see this issue. On Android, with the app in the background, and the notification sent through FCM, tapping the notification opens the app, but no event is raised. When the app is killed, it works, although getInitialNotification takes a long time before it runs.