React Native本地推送通知后台/关闭状态不触发,需点击通知跳转应用
解决React Native Push Notifications后台/关闭状态接收本地通知并点击打开应用的问题
核心问题修正
你的代码里有两个关键设置导致需求无法实现:
invokeApp: false:这个参数会阻止点击通知时唤起应用,必须改为true。- 通道优先级不匹配:创建通道时的
importance与通知配置的importance要统一,确保后台能触发通知。
修正后的代码
// 创建通知通道(建议在应用启动时执行,比如App.js根组件中) PushNotification.createChannel({ channelId: "alarm-channel", channelName: "Alarm Channel", importance: 4, // 对应Android的IMPORTANCE_HIGH,确保后台可触发通知 vibrate: true, }); const [selectedDate, setSelectedDate] = useState(new Date()); const handleDatePicker = (dateTime) => { const fireDate = dateTime; PushNotification.localNotificationSchedule({ channelId: "alarm-channel", title: 'title', id: alarmNotifData.id, message: 'message', date: fireDate, soundName: "Default", actions: ["Snooze", "Stop Reminder"], importance: 4, // 和通道的importance保持一致 playSound: true, allowWhileIdle: true, invokeApp: true, // 改为true,点击通知直接唤起应用 priority: "high", timeoutAfter: 50000, autoCancel: false, vibrate: true, vibration: 500, }); }; handleDatePicker(selectedDate);
额外配置要点
Android端
- 在
AndroidManifest.xml中添加必要权限:<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> - Android 12及以上版本必须创建通知通道,且通道
importance需设为3(IMPORTANCE_DEFAULT)及以上,否则后台无法显示通知。 - 若需要锁屏显示通知,可在通道配置中添加
lockscreenVisibility: 1(对应VISIBILITY_PUBLIC)。
iOS端
- 在
Info.plist中开启相关配置:<key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array> <key>NSLocalNotificationUsageDescription</key> <string>需要通知权限来提醒您的日程</string> - 应用启动时主动请求通知权限:
PushNotification.requestPermissions();
测试注意事项
- 关闭应用测试时,不要使用Debug模式(Debug模式下关闭应用会终止JS进程,影响本地通知触发),改用Release包测试。
- 确保设备通知权限已完全开启(包括锁屏显示、后台通知权限)。
内容的提问来源于stack exchange,提问作者Pankaj
相关产品推荐
相关产品推荐

