我一直在尝试使用 rnfirebase 在 React Native 中实现使用 FCM 进行通知,同时使用 notifee 处理本地通知。我已经能够通过 Firebase Cloud Messaging 接收到后台通知(即应用被杀死或最小化的状态),并可以使用 notifee 获取前台通知。我现在希望使用 notifee 处理后台通知以保持通知的一致性。以下是代码:
使用此代码可以获取前台通知。当应用程序最小化时,会收到来自notifee和FCM的两个通知。当应用程序被杀死时,只会收到FCM通知而不是notifee通知。
问题:
1. 如何在应用程序被杀死的状态下从notifee获取通知? 2. 如何禁用FCM后台通知?我需要只从Firebase发送数据通知吗? 3. 在One Plus设备上,由于显示应用程序未运行,因此无法在被杀死的状态下获取FCM通知。我需要在Android清单文件中添加什么?
解决方案:
Q1通过将setBackgroundHandler从useEffect内部移动到hook外部来解决。
Q2尚未解决。
Q3尚未解决。
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通知而不是notifee通知。
问题:
1. 如何在应用程序被杀死的状态下从notifee获取通知? 2. 如何禁用FCM后台通知?我需要只从Firebase发送数据通知吗? 3. 在One Plus设备上,由于显示应用程序未运行,因此无法在被杀死的状态下获取FCM通知。我需要在Android清单文件中添加什么?
解决方案:
Q1通过将setBackgroundHandler从useEffect内部移动到hook外部来解决。
Q2尚未解决。
Q3尚未解决。