react-native-push-notification: onNotification is not called when remote notification arrives and app is in background/foreground
I’m to update the UI when onNotification
arrives. In general onNotification
works as expected when app is killed. It wakes up -> onNotification
gets called.
BUT onNotification
doesn’t seem to be getting called when the app is NOT killed, i.e. in foreground or background.
I saw people reporting similar issues for local notifs, but I assume for remote notifications it’s different?
Can someone suggest what could be wrong?
import React from 'react'
import { Platform } from 'react-native'
import PushNotification from 'react-native-push-notification'
import PushNotificationIOS from '@react-native-community/push-notification-ios'
const Root = () => {
PushNotification.configure({
onRegister: pushToken => {
console.log(pushToken.token)
},
onNotification: notification => {
console.log('Notification received', notification)
notification.finish(PushNotificationIOS.FetchResult.NoData)
},
onError: err => {
console.log('Error configuring notifications', err)
},
senderID: SENDER_ID,
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: Platform.OS === 'android',
})
return <Router />
}
The library version:
"react-native-push-notification": "^3.5.0",
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 20
- Comments: 23
This solved it for me:
SplashActivity
MainActivity
AndroidManifest
@Dallas62 you were right. On the push-notification-ios#107 issue you eluded to the fix pointed out here enabled me to begin receiving onNotification events while the app was in the background or foreground. Thanks for your pointers!
Facing the same issue in IOS ,onNotification is not triggered but it is triggering if the user clicks on the recieved notification.need Onnotification to trigger as soon as we get the remote notification
I am also facing similar issue in IOS.
When app is in foreground / background,
onNotification
handler is not getting called when notification is received, though this is working perfectly fine in android.@alex-mironov we also having this issue - we are able to receive and show the push notification when app is in background. However, when app is in the foreground, push notification is not received and shown.
We have also added the following in AppDelegate.m
Please let us know if you find a solution to your issue
@Dallas62 thanks for the prompt reply. Yeah, sure,
SENDER_ID
is there, just dropped that import not to clutter the snippet.I also updated native part
As I mentioned, I was able to receive a plain notification when the app is killed, the only issue to make
onNotification
working when app is in the background/foreground.