I have a function that is called on onNotification. This function checks for link that comes in data and checks for user interaction to be true - if condition passes then navigation function is called to navigate to needed screen in the app.
All this looks something like:
PushNotification.configure({
....
onNotification: this._onNotification.bind(this),
....
});
_onNotification(notification) {
....
if (notification && notification.userInteraction) {
let data = notification.data || {};
if (data && data.link) {
//do navigation to link
}
}
}
This works all good with iOS where notification.userInteraction changes from false to true on notification click and app opening. The onNotification is called once more and passes condition, therefore does the navigation. With android as far as I got from debugging the app - onNotification is called only once and only on notification being received, when you click on notification to open app - onNotification is not updated and called once more.
My current RN version is 0.39 and current react-native-push-notification version is 2.2.1
Any ideas or suggestions that would help to resolve the issue? :)
I'm also seeing the app being opened when you click the notification, but onNotification not being called.
Me also. onNotification is called ONLY WHEN the app is running on foreground
I get this too, any solution?
Maybe it is not nice to promote other package, but I didn't find easy fix so switched to https://github.com/wix/react-native-notifications/
It is easier to implement(less stuff to do in manifest) and it has separated methods: setNotificationReceivedListener and setNotificationOpenedListener, so it is easier to handle notifications and separate logics of these two states.
Main concern for me was that iOS and Android functionality is not unified, but I've simply created Notifications.android.js and Notifications.ios.js to keep it separate, but leave import to handle which one is needed.
I am experiencing this as well. It appears that hasPoppedInitialNotification gets set to true on app start. Subsequently, when re-opening my app after notification interaction, I call configure() and the library does not check for an initial notification. I'm not sure what the expected behavior is -- I think I do not understand how this is supposed to be used, because it seems like the expectation here is that the JS environment for the app to be cleared out before it is possible to get another initial notification emitted in this way.
Has there been any progress on this at all? I'm seeing the same. App open - onNotification called. App closed or in the background - Notification shown in notification center. Clicking on notification does not result in onNotification being called though.
@npomfret @zo0r
Would appreciate your thoughts on this issue as this seems to exist for a long time
Hope this helps others. Here is how I got the my Android code to read in data from notification when launching app from background/killed state.
there are two different notification logics for foreground and background/killed apps.
onMessageReceived() is executed only for foreground apps. You can get “notification” and “data” by using remoteMessage.getNotification() and remoteMessage.getData().
Make sure your notification has 2 components in them
{
data: {
// Key value pairs that you need to read when app is launched
}
notificaiton: {
body: "" // text in the body of the notification UX,
title: "" // title of the notiifcation UX
}
}
Inside your MainActivity.java, do something along the lines of the following:
@Override
protected Bundle getLaunchOptions() {
Intent mainIntent = getIntent();
String dataValue = "";
if (mainIntent != null) {
Bundle bundle = mainIntent.getExtras();
Log.v("RNPushNotification", "Data received in main: " + bundle);
if (bundle != null) {
JSONObject data = getPushData(bundle.getString("data"));
if (data != null && data.has("dataKey")) {
try {
dataValue = data.getString("dataKey");
} catch (Exception e) {
// no-op
}
}
}
}
initialProps.putString("dataValue", dataValue); // Read this inside your Root component in React native
return initialProps;
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.
Most helpful comment
Has there been any progress on this at all? I'm seeing the same. App open - onNotification called. App closed or in the background - Notification shown in notification center. Clicking on notification does not result in onNotification being called though.