setBackgroundMessageHandler:如何在iOS上调用应用程序处于退出状态 &g



我正在使用react-native-firebase库。

在iOS上,我可以在前台、后台以及应用程序处于退出状态时接收通知。处理程序也可以在任何场景中工作,除了一个。

我唯一的问题是:当应用程序处于退出状态时,即使我收到通知,setBackgroundMessageHandler也没有被调用。我也试过只发送数据消息,但结果是一样的。

我甚至可以在消息的apns部分使用content-available: 1在无头状态下调用应用程序。

但是仍然没有调用setBackgroundMessageHandler。这是我的index.js页面:

messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('HEADLESS BACKGROUND: ' + ' | ' + JSON.stringify(remoteMessage));
});
function HeadlessCheck({ isHeadless }) {
if (isHeadless) {
console.log("Headless");
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
AppRegistry.registerComponent(appName, () => HeadlessCheck);

在无头调用的情况下,返回一个不包含任何内容也不呈现任何内容的简单组件,而不是返回null

Index.js

messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('HEADLESS BACKGROUND: ' + JSON.stringify(remoteMessage));
});
function HeadlessCheck({ isHeadless }) {
if (isHeadless) {
console.log('Headless');
return <AppFake />;
/* Notice this component, it is not the App Component but a different one*/
}
return <App />;
}
AppRegistry.registerComponent(appName, () => HeadlessCheck);

AppFake组件只包含如下内容:

import { useEffect } from 'react';
import SplashScreen from 'react-native-splash-screen';
const AppFake = () => {
useEffect(() => {
SplashScreen.hide();
}, []);
return null;
};
export default AppFake;

我必须隐藏SplashScreen。这对我来说很重要。

这是我从服务器发送的有效载荷:

var admin = require('firebase-admin');
const message = {
notification: {
title: title,
body: body,
},
data: { campaignId: campaignId, title: title, body: body },
token: registrationId,
android: {
priority: 'high',
notification: {
title: title,
body: body,
sound: 'default',
},
},
apns: {
payload: {
aps: {
sound: 'default',
'content-available': 1,
},
},
headers: {
'apns-priority': '5',
},
},
};
let response = admin
.messaging()
.send(message)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});

现在setBackgroundMessageHandler将在每条新消息上被调用,即使应用程序在iOS上处于退出状态。

最新更新