iOS应用被杀死状态下React Native推送通知接收异常问题
iOS 应用杀死后无法接收推送通知的解决方案
1. 确认APNs推送类型配置
- 推送 payload 必须包含
content-available: 1和mutable-content: 1(若需静默推送或自定义通知);普通通知需确保alert字段存在,iOS杀后台时只有带alert的通知才会主动展示;静默推送需仅设置content-available:1且无alert字段(此类推送仅触发后台任务,不弹出通知)。
2. 检查Xcode推送相关配置
- 打开项目
Info.plist,确认已添加UIBackgroundModes并包含remote-notification项,允许应用后台接收推送。 - 确认Xcode的
Signing & Capabilities中已开启Push Notifications权限,且对应的Provisioning Profile包含推送权限。
3. 修正代码逻辑问题
你当前代码在onNotification中调用PushNotificationIOS.addNotificationRequest是创建本地通知的操作,远程推送的处理逻辑存在错误:杀后台状态下,远程通知的展示由iOS系统负责,无需应用端创建本地通知;你的代码导致只有打开应用后才触发本地通知创建,这就是重启后收到积压通知的原因。
修改后的代码示例:
PushNotification.configure({ onRegister: function (token) { console.log("PUSH NOTIFICATION TOKEN:", token.token, token.os); }, onNotification: function (notification) { if (Platform.OS === 'ios') { // 前台状态下,iOS不会自动展示远程通知,可手动处理展示 if (notification.foreground) { PushNotificationIOS.presentLocalNotification({ alertTitle: notification.data.title, alertBody: notification.data.body, userInfo: notification.data }); } // 必须调用finish告知iOS通知处理完成 notification.finish(PushNotificationIOS.FetchResult.NoData); } }, // 配置推送权限,确保申请必要权限 permissions: { alert: true, badge: true, sound: true }, popInitialNotification: true, requestPermissions: true });
4. 验证推送Payload格式
正确的普通远程推送Payload示例(杀后台可正常展示):
{ "aps": { "alert": { "title": "通知标题", "body": "通知内容" }, "badge": 1, "sound": "default", "content-available": 1 }, "data": { "msgId": "123", "title": "通知标题", "body": "通知内容" } }
5. 检查设备端通知权限
在iOS系统设置中,确认应用的通知权限已完全开启:包括允许通知、锁定屏幕/通知中心/横幅展示权限,以及后台刷新权限。
内容的提问来源于stack exchange,提问作者Mr Abbasi
相关产品推荐
相关产品推荐

