我一直在使用 react-native-push-notification 库来获取本地通知。现在我需要远程静默通知。基本上,我发送一个通知,该通知传递到onNotification回调,并将本地通知发送到通知中心。
我希望每次收到通知时都会触发 onNotification 回调,并且我通过单击通知打开应用程序。问题是,根据我在哪里打电话给PushNotification.configure({...})
,在 Android 上,它会在通知送达或单击它时触发 onNotification,但绝不会两者兼而有之!我的代码:
// 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()
- 它只是库配置调用上的简单包装器。是的,试图将其保留在两个地方 - 不起作用。我做错了什么?
我总是在推送通知中发送数据有效负载。
文档说不要将配置放在 React 生命周期中。否则Android将不会有正确的行为(我在这一点上感到非常痛苦!这里有一个很好的教程:https://product.farewell.io/visible-react-native-push-notifications-that-work-on-both-ios-and-android-5e90badb4a0f解释不好,但方法很完美。使用类初始化和调用方法。按照他的建议初始化顶部组件中的类。效果很好。通过本教程完成缺少的信息:https://shift.infinite.red/react-native-node-js-and-push-notifications-e851b279a0cd玩得愉快!