react-native-push-notification: onNotification is not calling when app is in background
Bug
When I am calling fcm server push notification api data is not showing on onNotification function.
Environment info
react-native info
output:
// paste it here
Library version: 3.4.0
Steps To Reproduce
I use the following code for recieving push notification
- Call the service from postman collection for fcm
- we got data in onNotification when app is in foreground but not in backgorund …
Describe what you expected to happen:
Reproducible sample code
PushNotification.configure({ // (optional) Called when Token is generated (iOS and Android) onRegister: function (token) { console.log(“TOKEN:”, token); thats.setState({deviceToken:token}); },
// (required) Called when a remote or local notification is opened or received
onNotification: function (notification) {
console.log('** NOTIFICATION: **', notification);
if (Platform.OS === 'ios') {
if (notification.alert.length !== 0) {
//handleNotification(notification)
notification.finish(PushNotificationIOS.FetchResult.NoData);
}
} else {
//handleNotification(notification)
}
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
requestPermissions: true,
});
PushNotification.popInitialNotification((notification) => {
console.log(notification);
})
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 68 (6 by maintainers)
Commits related to this issue
- #1431 — committed to taymer/react-native-push-notification by taymer 4 years ago
- Merge pull request #1446 from taymer/master #1431 — committed to zo0r/react-native-push-notification by Dallas62 4 years ago
- Sync with upstream (#3) * Make sure to import PushNotificationIOS from react-native first * typo * Delete versions.yml * Update push-notification-ios to 1.2.0 Version 1.1.0 giving a `No k... — committed to fcbtech/react-native-push-notification by vaibhavhrt 4 years ago
- Merge commit '1d497e60a142b4a846907ec19b80ab72a5ae5c8d' * commit '1d497e60a142b4a846907ec19b80ab72a5ae5c8d': (109 commits) Bump to 3.5.2. Remove extra interface. Fix sounds volume and DND is ig... — committed to minhnc/react-native-push-notification by minhnc 3 years ago
This is why
onNotification
is not trigger. Look at my previous message.You can’t use
notification
field with background handler. You must usedata
(and not combined withnotification
). Then if you want to notify the user, trigger a local notification.Hi,
There is no channelId defined.
Hi @romelbonnie There is an
userInteraction
field in the notification object. true == user has pressed the notification. Regards,I am still facing this problem. can you give some example code? Thank you
Hi @kirantripathi
https://github.com/zo0r/react-native-push-notification/issues/1431#issuecomment-642441772
Hi, If you want a background process in any cases, you must send data-only notification. This way you should be able to trigger a local notification and run your process. Persist your state at the end of the process. Long process are not allowed (not sure but it’s <10/20s), prefer send data in notification than requesting a server when possible.
Hi @mehul2013 On the current version of the library, scheduled notification doesn’t trigger
onNotification
. You can test the version ondev
branch but some changes will be made before the release. I try to update theCHANGELOG.md
ondev
as soon as changes are made.@taymer I understand. Do you have any tips/suggestions for me? I need to call onNotification ALWAYS when a notification is received to update a redux state to add a notification badge number on my hamburger button, so the user will know if and how many notifications he has. I was progressing well but got stuck on this. Check the badge I’m using.
@Dallas62
singleTop
.I do not remember the exact reason why though. I feel like I’ve had to mess with that a few times over the years. I wrote a particularly useless commit message for that too…
EDIT: I ended up making the following change. Not an Android developer, so I don’t know if it makes a real difference, but at least it is not broken. Then again, the original worked for me as well.