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

React Native FCM纯数据消息无法接收推送通知问题求助

自定义远程推送通知无接收问题排查

问题现象

移除notification字段后,设备完全收不到纯数据消息,setBackgroundMessageHandler和onMessage无日志输出,模拟器与真机测试均无效。

发送的消息格式

{
  "data": {
    ...
    "body": "[phone] approved your proposed S$20.00 contribution",
    "title": "App Name",
    "content-available": true
  },
  "from": "275845609097",
  "messageId": "1712017406867021",
  "apns": {
             "payload": {
                 "aps": {
                     "content-available": true
                 }
             },
             "headers": {
                 "apns-push-type": "background",
                 "apns-priority": "5",
                 "apns-topic": "com.*.*"
             }
         },
  // "notification": {
  //    "body": "[phone] approved your proposed S$20.00 contribution",
  //    "title": "App Name"
  //  }
  "android": {
      "priority": "high"
    }
}

环境版本

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

排查与修复方案

iOS端配置修正

  • APNs推送类型调整:apns-push-type设为alert而非background,background仅用于静默后台更新,不会触发消息回调。
  • aps字段补充:在aps中添加空alert结构,否则iOS可能忽略纯数据消息:
    "aps": {
      "content-available": 1,
      "alert": {}
    }
    
  • 优先级调整:apns-priority改为10,确保消息及时送达。
  • topic修正:apns-topic替换为实际的App Bundle ID,不能使用通配符com.*.*。

Android端配置检查

  • 数据消息映射:确保自定义数据正确放在根级data字段中,android字段可补充data映射保证兼容性:
    "android": {
      "priority": "high",
      "data": {
        "body": "[phone] approved your proposed S$20.00 contribution",
        "title": "App Name"
      }
    }
    
  • 权限与服务确认:检查设备已授予后台通知权限,AndroidManifest.xml中已注册ReactNativeFirebaseMessagingService。

React Native Firebase回调配置

  • 后台回调注册时机:在App启动时立即注册setBackgroundMessageHandler,且返回Promise:
    import messaging from '@react-native-firebase/messaging';
    
    messaging().setBackgroundMessageHandler(async remoteMessage => {
      console.log('Background message handled:', remoteMessage);
      return Promise.resolve();
    });
    
  • 前台监听与权限:确保已请求用户通知权限,并正确注册onMessage:
    async function setupNotifications() {
      const authStatus = await messaging().requestPermission();
      const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL;
      
      if (enabled) {
        messaging().onMessage(async remoteMessage => {
          console.log('Foreground message received:', remoteMessage);
        });
      }
    }
    
    setupNotifications();
    

通用验证项

  • 检查设备令牌是否正确,发送消息的Firebase项目与App配置一致。
  • 确保data字段无语法错误,所有自定义参数格式合法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 18:35:11