如何解决react-native firebase fcm setBackgroundMessageHandler 不工作
我正在创建一个函数,用于在 FCM 的 setBackgroundMessageHandler 事件中通过异步存储存储数据。
我正在使用@react-native-firebase/messaging。 SetBackgroundMessageHandler 不起作用。
我遵循了 https://rnfirebase.io/messaging/usage/ios-setup 上的教程并从 firebase 网站发送了它。
除了当前的 setBackgroundMessageHandler 函数外,一切正常。
我找了很久,还是找不到答案。
我用真实设备对其进行了测试。 推送消息正常显示,但是没有发生setBackgroundMessageHandler事件。
Xcode BackgroundMode ScreenShot
package.json
"@react-native-firebase/app": "^11.2.0","@react-native-firebase/messaging": "^11.2.0","@react-native-firebase/ml": "^11.2.0",
index.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);
App.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
}
});
},[]);
解决方法
我的代码没有问题,Firebase 提供的云消息测试工具没有正常到达。
已通过 OAuth 2.0 Playground 成功生成令牌并将消息发送给邮递员。 https://developers.google.com/oauthplayground
下面的代码是成功转移到postman的结构。 https://fcm.googleapis.com/v1/projects/[firebase-project-id]/messages:send
{
"message": {
"token": "device token]","notification": {
"title": "message title","body": "hello background worker?"
},"apns": {
"payload": {
"aps": {
"content-available": 1
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。