[英]How to recieve background notifications in notifee and FCM
我一直在尝试通过 rnfirebase 在 React Native 中使用 FCM 实现通知。 并使用 notifee 处理本地通知。
我已经能够通过 firebase 云消息传递接收后台通知,即终止状态和最小化状态,并且能够使用 notifee 获取前台通知。
现在我想将 notifee 用于后台通知以确保通知之间的一致性。
这里的代码
const displayNotification = async () => {
const channelId = await notifee.createChannel({
id: 'important',
name: 'Important Notifications',
importance: AndroidImportance.HIGH,
});
notifee.displayNotification({
body: 'This message was sent via FCM!',
android: {
channelId: channelId,
actions: [
{
title: 'Mark as Read',
pressAction: {
id: 'read',
},
},
],
},
});
};
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
displayNotification();
});
messaging().onMessage(async remoteMessage => {
console.log('Message handled in the foregourp!', remoteMessage);
displayNotification();
});
使用此代码获取前台通知。 当应用程序最小化时,收到两个通知,一个来自 notifee,另一个来自 FCM。 当应用程序被杀死时,只会收到 FCM 通知而不是通知通知。
问题
解决方案
Q1 通过将 setBackgroundHandler 从 useEffect 内部移到挂钩外部来解决。
Q2仍在等待中
Q3仍在等待中
backgroundMessageHandler 使用来自 FCM 的信息自行创建一个通知,触发您自己的通知只会导致两个通知。 你应该删除displayNotification();
在后台消息处理程序中。 此外,backgroundMessageHandler 应该位于index.js
文件中,而不是您的应用程序。
如果想忽略FCM后台通知,忽略notification参数即可。 只需发送 data 参数,但 Data-only 消息在 Android 和 iOS 上都以低优先级发送,默认情况下不会触发 setBackgroundMessageHandler。 要启用此功能,您必须在 Android 上将“优先级”设置为高,并在消息负载中为 iOS 启用内容可用标志。
{
"to": "User_Device_Token",
"data": {
"body": "Body of Your Notification",
"title": "Title of Your Notification"
},
"priority": "high",
"contentAvailable": true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.