使用react-native-push-notification发本地通知时OnNotification()不触发如何解决
react-native-push-notification本地通知无法触发OnNotification()解决方案
- 检查本地通知调用参数是否携带非空
userInfo字段
该库存在内置逻辑限制:本地通知未携带userInfo字段时会直接跳过OnNotification回调触发,调用时至少传入空对象即可,参考示例:
PushNotification.localNotification({ title: "通知标题", message: "通知内容", userInfo: {}, // 必须添加该字段,即使内容为空 // 其余自定义参数 })
- 检查前台场景的
foreground参数配置
应用处于前台运行状态时,必须在通知参数中显式指定foreground: true才会触发OnNotification回调:
PushNotification.localNotification({ title: "前台通知", message: "前台运行时可触发回调", userInfo: {}, foreground: true // 前台场景必填 })
- 检查安卓端清单文件配置
确认AndroidManifest.xml中已正确配置本地通知对应的接收器动作,不要遗漏notification_receive的intent配置:
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationReceiver" android:exported="false"> <intent-filter> <action android:name="com.dieam.reactnativepushnotification.notification_action" /> <action android:name="com.dieam.reactnativepushnotification.notification_receive" /> </intent-filter> </receiver>
- 检查回调绑定时机和初始化配置
OnNotification回调必须在PushNotification.configure()方法初始化时直接传入绑定,不要延迟到组件挂载后再动态赋值,同时确认配置中requestPermissions设为true,已正常获取通知权限。 - 检查高版本的回调完成标记
7.0.0及以上版本的库,回调处理完成后必须调用notification.finish()方法标记处理结束,否则后续回调可能被系统判定为未完成而阻塞触发:
PushNotification.configure({ onNotification: function(notification) { // 自定义通知处理逻辑 console.log("收到通知", notification); // 处理完成后调用finish标记 notification.finish(); }, // 其余配置项 requestPermissions: true })
内容的提问来源于stack exchange,提问作者Chetan Bawankule
相关产品推荐
相关产品推荐

