You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS端@react-native-firebase/messaging getInitialNotification返回null求助

iOS下Firebase Messaging getInitialNotification返回null的解决方案

环境配置

package.json依赖:

"@react-native-firebase/app": "^18.3.0",
"@react-native-firebase/messaging": "^18.3.0",

问题复现

Android平台调用以下代码可正常获取启动应用的通知,但iOS平台message始终返回null:

// App.js
firebase.messaging().getInitialNotification().then(message => {
  console.log(
    'Notification caused app to open from quite state:',
    message,
  );
  if (message != null && message.data) {
    Common.handlePushNavigation(this.props.navigation, { type: message.data.type, target: message.data.target });
  }
});

解决方案

1. 核对iOS推送基础配置

  • 确认Firebase控制台已上传正确的APNs证书/密钥,且证书对应的bundle ID与项目完全匹配
  • 打开Xcode项目的Signing & Capabilities,确保Push Notifications已启用,同时Background Modes中勾选Remote notifications

2. 调整通知Payload格式

iOS要求触发getInitialNotification()的通知必须是可见通知,不能是纯数据消息:

  • Payload必须包含alert字段(可设为空对象{}),示例有效Payload:
    {
      "notification": {
        "title": "测试通知",
        "body": "点击打开应用"
      },
      "data": {
        "type": "news",
        "target": "123"
      },
      "apns": {
        "payload": {
          "aps": {
            "mutable-content": 1
          }
        }
      }
    }
    
  • 纯数据消息(无notification/alert字段)属于静默通知,iOS不会唤醒应用,无法通过getInitialNotification()获取

3. 代码层面优化

  • 将getInitialNotification()调用放在应用启动的最早期,比如类组件的componentDidMount或函数组件的useEffect中,避免延迟调用
  • 确保已提前请求通知权限,无权限时iOS不会传递通知到应用

4. 修正测试方式

  • 改用真实设备测试,模拟器对推送支持有限
  • 避免在Xcode调试模式下测试,先打Ad Hoc包或通过TestFlight安装应用,彻底杀死应用(从多任务界面滑掉)后再发送通知测试

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 10:22:26