问题
I've been using react-native-push-notification library for quite some time for local notifications. Now I need remote silent notifications. Basically I send a notification that gets delivered to onNotification callback and I send local notification to the notification center.
I expect onNotification callback to trigger every time I receive notification and I open an app by clicking on notification. The problem is, depending on where I call PushNotification.configure({...})
, on Android it triggers onNotification when notification gets delivered OR when I click on it, but never both! My code:
// if here: calls onNotification when it's received
PushNotifications.configureNotifications();
export default class App extends React.Component {
constructor(props) {
// if here: calls onNotification when I click on it to open an app
PushNotifications.configureNotifications();
super(props);
}
render() { ... }
}
PushNotifications.configureNotifications()
- it's just a simple wrapper over the library configure call. Yes, tried to keep it in both places - doesn't work. What am I doing wrong?
I always send data payload in push notification.
回答1:
Documentation says to not put the configuration in the React life-cycle. Otherwise Android won't have the right behavior (I had so mush pain with this point !). Here you have a nice tutorial: https://product.farewell.io/visible-react-native-push-notifications-that-work-on-both-ios-and-android-5e90badb4a0f It's bad explained, but the approach is perfect. Use a class to initialize and call the methods. Initialize the class in the top component as he suggests. It works good. Complete the missing informations with this tutorial: https://shift.infinite.red/react-native-node-js-and-push-notifications-e851b279a0cd Have fun !
来源:https://stackoverflow.com/questions/46978190/react-native-push-notification-onnotification-callback-is-inconsistent