React-Native iOS应用无法接收仅数据消息(content_available失效)
iOS端切换Notifee仅数据消息后无响应问题
免责声明:此问题与多篇帖子类似,但我已尝试所有给出的解决方案均无效。
此前我使用原生推送通知并通过react-native-firebase处理,一切运行正常。添加Notifee并切换为仅数据消息后,iOS端无任何响应,无论应用处于前台还是后台,消息仿佛都未送达。
JSON负载
{ "to" : "FCM_TOKEN", "content_available":true, "apns": { // <=== 该对象无作用 "payload": { "aps": { "alert": { "title":"hello ios", "body":"world ios" } } } }, "priority":"high", "data":{ "notifee": { "title":"Hello", "body":"World", "data": { "name": "Nick" } } } }
useNotifications钩子实现
// 前台处理器 const handleIncoming = async (remoteMessage: RemoteMessage) => { console.log('=== Incoming ===', remoteMessage); // <=== 未触发 // ... 其他操作(无关,未执行到此处) }; // 后台处理器 const handleTapToOpenNOTIFEE = async (event: Event) => { console.log('=== EVENT ===', JSON.stringify(event)) // <=== 未触发 // ...其他操作(无关,未执行到此处) }; useEffect(() => { // 处理应用前台打开时的消息 messaging().onMessage(handleIncoming); // 处理应用后台运行时的事件 notifee.onBackgroundEvent(handleTapToOpenNOTIFEE); }, [allowPushNotifications, hasAccessToNoticeBoard]);
index.js中的实现
messaging().setBackgroundMessageHandler(async (remoteMessage) => { console.log('remoteMessage in BG handler', remoteMessage); // <=== 未触发 await notifee.requestPermission(); const channelId = await notifee.createChannel({ // ... Android渠道配置 }); const parsed = JSON.parse(remoteMessage.data.notifee || ''); await notifee.displayNotification({ // <--- 未执行到此处 title: `${parsed?.title}`, body: `${parsed?.body}`, data: parsed.data, android: { channelId, color: AndroidColor.LIME, smallIcon: 'ic_small_icon', pressAction: { id: 'default', }, }, }); });
包版本
react-native: 0.64.2@notifee/react-native: 7.8.0@react-native-firebase/messaging: 15.4.0
总结
从后端识别设备类型并调整负载显然不是理想方案,但现在甚至觉得这也可行😂。如前所述,我已排查所有类似问题但均未解决,感谢您能提供的任何帮助。
已尝试的修复方案
- 在负载中添加
content_available、content-available和contentAvailable - 在负载中添加APNS对象
- 添加请求头:
{ apns-push-type: background, apns-priority: 5, apns-topic: <DOMAIN_NAME> } - 从负载中移除
priority: true(根据Stack Overflow建议)
我看到不同文档中
content_available属性的大小写不同,RNFirebase文档显示为content-available,网上多数解决方案为content_available,但两者对我均无效。
预期结果
通知应触发RNFirebase的setBackgroundMessageHandler方法,随后Notifee根据传入的负载显示本地通知。
内容的提问来源于stack exchange,提问作者nburczyk-cs
相关产品推荐
相关产品推荐

