反应本机 firebase fcm setBackgroundMessageHandler 不工作

k22*_*2pr 5 javascript firebase-cloud-messaging react-native-firebase

我正在创建一个函数,通过 FCM 的 setBackgroundMessageHandler 事件中的异步存储来存储数据。

我正在使用@react-native-firebase/messaging。SetBackgroundMessageHandler 不起作用。

我按照https://rnfirebase.io/messaging/usage/ios-setup上的教程操作,并从 firebase 网站发送了它。

除了当前的 setBackgroundMessageHandler 函数之外,一切正常。

我找了很久,却找不到答案。

我用真实设备进行了测试。推送消息正常显示,但没有发生setBackgroundMessageHandler事件。

Xcode 背景模式屏幕截图

包.json

"@react-native-firebase/app": "^11.2.0",
"@react-native-firebase/messaging": "^11.2.0",
"@react-native-firebase/ml": "^11.2.0",
Run Code Online (Sandbox Code Playgroud)

索引.js

import React from 'reactn';
import messaging from '@react-native-firebase/messaging';
import {AppRegistry} from 'react-native';
import App from './App';

messaging().setBackgroundMessageHandler(async remoteMessage => {
  // ...is not working
  console.log('Message handled in the background!', remoteMessage);
});

function HeadlessCheck({isHeadless}) {
  if (isHeadless) {
    return null;
  }

  return <App />;
}

AppRegistry.registerComponent('AppName', () => HeadlessCheck);
Run Code Online (Sandbox Code Playgroud)

应用程序.tsx

  React.useEffect(() => {
    var _resultList = resultList;

    const unsubscribe = messaging().onMessage(async remoteMessage => {
      //...is working
    }

  messaging().onNotificationOpenedApp(remoteMessage => {
    //...is working
  });

  // Check whether an initial notification is available
  messaging()
    .getInitialNotification()
    .then(remoteMessage => {
      if (remoteMessage) {
        //...is working
      }
    });

  }, []);
Run Code Online (Sandbox Code Playgroud)

k22*_*2pr 2

我的代码没有问题,Firebase提供的云消息测试工具没有正常到达。

通过 OAuth 2.0 Playground 成功生成令牌并将消息发送给邮递员。 https://developers.google.com/oauthplayground

下面的代码是成功传输给postman的结构。 https://fcm.googleapis.com/v1/projects/[firebase-project-id]/messages:发送

{
    "message": {
        "token": "device token",
        "notification": {
            "title": "message title",
            "body": "hello background worker?"
        },
        "apns": {
            "payload": {
                "aps": {
                    "content-available": 1
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)