即使在iOS中发送了通知,setBackgroundMessageHandler也不会返回任何内容



我使用@react native firebase/messaging进行推送通知,但是,由于某种原因,当我使用模拟器和真实的iphone进行调试时,即使通知已在iOS中设置,我也无法让setBackgroundMessageHandler打印console.log。我试着学习firebase云消息的教程。

import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import App from './src/App';
import {name as appName} from './app.json';
// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
AppRegistry.registerComponent(appName, () => App);

我把所有后台获取、远程通知和后台处理都安排好了。如有任何帮助,将不胜感激

首先,您需要配置headless

无头文件

  • 示例
// index.js
import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
function HeadlessCheck({ isHeadless }) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
function App() {
// Your application
}
AppRegistry.registerComponent('app', () => HeadlessCheck);

第二,对于iOS特定的";"仅数据";消息,消息必须包括适当的APN头以及content-available标志,以便触发后台处理程序。例如,如果使用Node.js firebase管理包发送一个";"仅数据";发送到iOS设备的消息:

  • 示例
admin.messaging().send({
data: {
//some data
},
apns: {
payload: {
aps: {
contentAvailable: true, // add this !!
},
},
headers: {
'apns-push-type': 'background',
'apns-priority': '5',
'apns-topic': '', // your app bundle identifier
},
},
//must include token, topic, or condition
//token: //device token
//topic: //notification topic
//condition: //notification condition
});

相关内容

  • 没有找到相关文章