我在我的react-native应用程序中使用firebase/messaging来接收来自firebase的纯数据通知。
收到消息后,如果应用程序处于后台或已终止,我将处理数据,更新状态并使用react-native-push-notification显示本地通知。
messaging().setBackgroundMessageHandler(async remoteMessage => {
PushNotification.localNotification({...})
})
在 Android 设备上,一切在前台、后台和终止状态下都能完美运行。
在iOS中前台和后台都可以工作。
当应用程序终止时,我可以在控制台中记录通知(见下文)并且有通知声音。但没有显示本地通知。
当我尝试记录 iOS 设备时,控制台打印以下输出
default 03:51:53.697535+0100 dasd Daemon Canceling Activities: {(
com.apple.pushLaunch.de.myapp.app:D1FAA2
)}
default 03:51:53.697770+0100 dasd CANCELED: com.apple.pushLaunch.de.myapp.app:D1FAA2 at priority 10 <private>!
我来自服务器的消息是:
const message = {
data: {
body: JSON.stringify(msgBody),
title: title,
},
contentAvailable: true,
priority: "high",
android: {
priority: "high",
},
apns: {
headers: {
"apns-priority": "10",
"apns-push-type": "alert", //already tried background too
"apns-topic": "de.myapp.app",
},
payload: { aps: { sound: "default", contentAvailable: true } },
},
tokens: userTokens,
};
这让我发疯好几天了,我无法让它工作。 感谢您的每一次帮助
Version 7.18.0 - August 13, 2020 Cloud Messaging
Deprecated setBackgroundMessageHandler. Use the new API onBackgroundMessage instead.
https://firebase.google.com/support/release-notes/js#cloud-messaging_2
查看他们的文档,他们提到系统在某些条件下有意保留,但不限于......
当系统收到新的后台通知时,会丢弃 较旧的通知,仅保留最新的通知。
如果某些因素强制退出或杀死应用程序,系统会丢弃
已通知。
如果用户启动应用程序,系统会立即发送
已通知。
我现在明白这个问题了。您似乎面临着在 iOS 上处理处于终止状态的通知的挑战,特别是与使用适当的 APN 标头和内容可用标志发送“仅数据”消息以触发后台处理程序有关。
如果您希望在终止或终止状态下收到通知时执行任务或进行 API 调用,则应考虑发送两个单独的通知。第一个通知应包含带有通知部分的有效负载,确保即使应用程序处于后台或终止状态,它也会触发 iOS 的通知弹出窗口。第二个通知将是“仅数据”消息,它必须包含必要的 APN 标头以及内容可用标志。此配置对于激活后台处理程序至关重要。
例如,当使用 Node.js firebase-admin 包向 iOS 设备发送“仅数据”消息时,请确保相应地构建有效负载。这种方法允许您实现在应用程序处于后台或终止状态时显示通知并触发后台处理程序进行进一步处理的所需行为。
注意有效负载结构的细节至关重要,包括可用内容和必要的 APN 标头的使用,以确保功能正常。建议在不同场景(包括前台、后台和终止状态)中进行测试,以验证您的实现是否按预期运行。
import messaging from '@react-native-firebase/messaging';
import React, { useEffect } from 'react';
import { AppRegistry, AppState } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('====================================');
console.log({ 'in kill state': remoteMessage });
// api call for status update
// you can perform you desired task
});
function HeadlessCheck({ isHeadless }) {
console.log('background is called', isHeadless);
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return <FakeView />;
}
return <App />;
}
const FakeView = () => {
return null;
};
AppRegistry.registerComponent(appName, () => App);
// Register the Headless task
AppRegistry.registerHeadlessTask(
'RNFirebaseBackgroundMessage',
() => HeadlessCheck,
);
这是我从服务器发送的有效负载:
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',
},
},
};