I'm having an issue with the onNotification callback not being called if the app is killed and the notification is clicked. Is there anyway to implement this functionality in the app?
RN -> 0.30.0
react-native-push-notification -> 2.0.2
same here
Ok, so I managed to work around the issue. However, I'm running a version behind you guys, but it might still work for you.
RN -> 0.29.0
react-native-push-notification -> 2.0.1
PushNotification.configure({
onRegister: (response) => this.onRegister(response.token),
onNotification: (notification) => this.handleNotification(notification),
popInitialNotification: false,
...
})
And in my onRegister method, I did:
onRegister(token) {
PushNotification.popInitialNotification((notification) => {
if (notification) { this.handleNotification(notification); }
});
...
}
@jakkra note that the notification format from popInitialNotification is different from the one in onNotification because it does not pass through the internal _onNotification
RN -> 0.30.0
react-native-push-notification -> 2.0.2
Platform: IOS (not tested on Android)
same issue here.. it was fine if the notification was received during app is in background mode.
Only when app is killed, received a notification message then user click on the notification from notification area, the payload data is not transferred to onNotification callback (as below)
{
foreground: false,
userInteraction: true,
message: undefined, // <--- bug???
data: undefined, // <--- bug???
badge: undefined,
alert: undefined,
sound: undefined
}
This problem can be solved by making some changes in native code.
In RNPushNotification.java file implement LifecycleEventListener like this:
public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener,LifecycleEventListener {
public RNPushNotification(ReactApplicationContext reactContext) {
super(reactContext);
....
reactContext.addLifecycleEventListener(this);
....
}
...
.@Override
public void onHostResume() {
Activity activity = getCurrentActivity();
if (activity != null) {
Intent intent = activity.getIntent();
Bundle bundle = intent.getBundleExtra("notification");
if (bundle != null) {
bundle.putBoolean("foreground", false);
bundle.putBoolean("userInteraction", true);
mJsDelivery.notifyNotification(bundle);
}
}
}
@Override
public void onHostPause() {
// Activity `onPause`
}
@Override
public void onHostDestroy() {
// Activity `onDestroy`
}
...
}
That's all and onNotification callback will be triggered everytime
@japesh thanks for this. Would you like to submit a PR with it in? If so, in the description it would be helpful if you could include a detailed description of the expected change in behaviour and how to test it.
hey @npomfret
this module works amazingly, but i am getting problem when app is closed completely. In this case the on notification function get trigger only ones, when notification is received. but when app is in background it trigger two times first when notification is received and second time when you click on notification.
the problem is that RNPushNotification class implements ActivityEventListener, and when application is closed their is no activity. Therefore there is no meaning of receiving call in onNewEvent function, that's what i think.
here comes the role of LifecycleEventListener it triggers it's function according to life events of activity. it trigger it's onresume function when the activity get open. therefore what is happening in onNewIntent function, i implemented in on resume function also.
in this way i start getting call in onNotification function when app is closed, both times first when i receive the notification and second when notification is clicked.
I will make a pull request if it worth.
thank you
@japesh Hey, just to make sure I'm getting this right, when I receive a notification and the app is closed, react-native-push-notification native code is runs in order to build a notification object and place it in the notification centre.
Now if I click this notification the app is started and the notification is available in popInitialNotification.
Is this not the desired behaviour?
What is the other trigger I'm missing?
@kfiroo but i was getting null in popInitialNotification.
I believe this is implemented for android now, but cannot possibly work on iOS. Can we close the issue?
@japesh Hi, your trick worked for me when clicking notification in background. However, when the app is in foreground, clicking notification is calling onNotification callback twice.
Did you have experimented this issue?
Have you checked their ids, are they belong to same notification or different.
@japesh Hi, they are different notifications. First, the app is in background mode (killed), so I send a PN, onMessage is called, I click notification, the app opens and onMessage is called again (with userInteraction=true).
After, I just close the app, and send a new PN, onMessage is called just for that new notification, but when I clicked the notification the app opens and onMessage is called twice, one time for the first notification and again for the new one.
I was getting same issue in that if we open app for the first time it was maintaining the state of first notification. For that I worked on the basis unique I'd. Whatever your functionality is, implement it only when you receive a unique Id. For that you can use an array of ids which have been used for opening notification.
Maybe it's happening because this? #281
You can confirm it by checking the id of notification. When i check, one of the notification Id was always same and that is of initial notification, that is notification received when your app is not even in background.
Any final solution for @japesh trick ?
Edit: Even with your hack, my onNotification is never called when my app is in background or killed.
@theohdv same for me. I'm not getting any onNotification when app is bg or closed either.
But locale notification works properly. So the problem should be with remote notifications.
@theohdv Can you post code snippets for your local notifications working correctly? Right now, that's all I'm using, but I still don't have onNotification called (just in Android).
PushNotification.localNotificationSchedule({
message: "My Notification Message", // (required)
date: new Date(Date.now() + (10 * 1000)) // in 10 secs
});
I have similar code to trigger the notification. How about onNotification or any tweaks you made to Android manifest or native code? RNPN version? RN version?
Much obliged.
I did not change the native code.
"react": "16.0.0-alpha.12",
"react-native": "0.45.1",
You maybe should read the Troubleshooting doc. Interesting infos about this module !
I am using react native version 0.43.3 and react-native-push notification version 3.0.0. For me the notification inside the popInitialNotification is always null inside iOS device.
public static startConfiguration(userId: string) {
if (PushNotification !== null) {
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
onNotification: (notification) => {
console.log('Notification received');
PushNotificationManager.handleNotificationReceived(notification);
},
onRegister: (tokenObject) => {
PushNotification.popInitialNotification((notification) => {
if (notification) {
PushNotificationManager.handleNotificationReceived(notification);
}
});
},
popInitialNotification: false,
senderID: ServerUtil.FireBaseSenderId,
});
}
}
The notification inside popInitialNotification is always null. I am calling the above startConfiguration() inside componentWillMount of my root component. I get the notificaiton correclty when my iOS app is in background or in foreground, but not when it is killed.
Any thoughts on what I am missing? Also opened an issue
@jakkra this solution still works in 3.0.2 (Android), however popInitialNotification needs to called from the UI (a component) rather than onRegister event handler
Hi @Gp2mv3, why was this issue closed?
I have the same issue, onNotification is not called
I have the same issue, onNotification is not called
+1 for only Ios
Hi @Mura7
Are you sure it's related to this library ?
Take a look at push-notification-ios.
Hi @nollymar
Which plateform you encounter this issue ?
A released has been made, please check the installation guide to be sure it's up to date.
Thanks
Hi @Mura7
Are you sure it's related to this library ?
Take a look at push-notification-ios.
Hi @Dallas62
thanks for fast answer.
local notification was not working for ios. I upgraded the Firebase package(6.4.1-alpha.0). now works.
Could it be affecting firebase also onNotification function?
Android works.
@Mura7 There is an issue react-native-community/push-notification-ios#106 on addEventListener.
The library is a simple proxy for this part, and doesn't include iOS specific implementation:
index.js#L84
this.callNative( 'addEventListener', [ 'notification', this._onNotification ] );
@Dallas62 It happens in iOS
Hi @nollymar
Which plateform you encounter this issue ?
A released has been made, please check the installation guide to be sure it's up to date.
Thanks
@nollymar So it's the same answer as Mura7:
https://github.com/zo0r/react-native-push-notification/issues/161#issuecomment-617674536
Most helpful comment
Ok, so I managed to work around the issue. However, I'm running a version behind you guys, but it might still work for you.
RN -> 0.29.0
react-native-push-notification -> 2.0.1
And in my onRegister method, I did: