Testing Matrix:
React-Native Version: 0.39.2
react-native-push-notification Version: 2.2.1
Android Version: 6.0.1
Phone Version: SAMSUNG-SM-G900V
Issue:
When the app has been opened and placed into the background or screen has been locked with the app in the background and a push notification is received once clicked the onNotification code does not run to display an alert message box.
If the app is closed or in the foreground and a push notification is received the app launches if closed and the onNotification code runs as expected by displaying an alert message box.
Could not duplicate this issue for iOS.
I've observed this issue when trying to use an updated version of google play services. Reverting to 8.1.0 fixed it for me. But obviously this isn't necessarily a workable solution for others.
Perhaps you have mixed versions of google api services in your dependencies. This happened to me, I think by using rn-maps.
I think the information I provided was not clear enough, in the onNotification I have an alert message box that appears fine when the app is closed as follows the app opens and displays the alert message box. When the app is in the foreground the alert message box appears without any issue.
This is the actual problem:
When the app has been placed in the background the small notification appears you click the box and it launches the app but no alert message from the onNotification is appearing as you would expect when the app is closed.
I have the same problem. Everything was working great with the following setup:
platform: Android,
react-native: 0.39.2
react-native-push-notification: 2.2.1
gcm: com.google.android.gms:play-services-gcm:8.1.0
tested on: Android 5.1, 6.0.1, 7.0
With this setup the onNotification is called in all possible cases, when app is in foreground, background and when app it's closed.
When I update the play-service-gcm to > ver. 9.0.0, ex. 9.2.1 the onNotification is called only when the app is in foreground. I've also tried to migrate from GCM to FCM but again, my app just gets the onNotification called only when the app is in foreground.
Does someone has a working example with google play services > 9 and onNotification to be called also when app is closed or in background?
Same here.
I can't say for sure or that it will work in every instance but I think I found a solution that seems to work and is a bit of a work around, the onNotification appears to be triggered before the app has been moved back to the foreground from the background. so I wrapped the message inside of onNotification inside of a setTimeout such as follows:
onNotification(notification) {
setTimeout(() => {
Alert.alert(
'Alert',
notification,
[{ text: 'Ok' }]
);
}, 1000);
}
I hope this helps with anyone else that may be experiencing this android background app push notification problem.
Hmm, this sounds like a weird solution, and introduces a race condition; on a slow phone, this still might not work.
Is there a way to register this to be executed as soon as the app is fully started?
I'm still dealing with this issue in library version 3.0.0. @GeoffreyPlitt is correct about the previous post just being a simple workaround. We should have a real solution implemented.
Most helpful comment
Hmm, this sounds like a weird solution, and introduces a race condition; on a slow phone, this still might not work.
Is there a way to register this to be executed as soon as the app is fully started?