在React Native Cloud Messaging、安静和后台模式下,会出现两个通知(一个来自Firebase,另一个来自Notifee)。当我点击notifee通知应用程序不打开,但如果点击firebase通知应用程序打开,也可能重定向特定的屏幕。我怎么能把这个实现到notifee notificaton中。
=======这是我的onDisplayNotification(notifee)函数========
export async function onDisplayNotification(data:any) {
// Request permissions (required for iOS)
if (Platform.OS === 'ios') {
await notifee.requestPermission();
}
// Create a channel (required for Android)
const channelId = await notifee.createChannel({
id: data?.data?.channel_id,
name: data?.data?.channel_name,
sound: data?.data?.sound_name,
// importance: AndroidImportance.HIGH,
});
// Display a notification
await notifee.displayNotification({
title: data?.notification.title,
body: data?.notification.body,
android: {
smallIcon: 'ic_launcher',
channelId,
color: BlueColor,
},
});
}
==========这是我在主页屏幕中调用的noficationListner函数。
export async function notificationListner() {
//=========== Foreground state messages(when user using app open stage)===========
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', remoteMessage);
//============= showing notification on notifee ===============
await onDisplayNotification(remoteMessage);
});
//=========== Foreground state messages(when user using app open stage)==============
messaging().onNotificationOpenedApp(remoteMessage => {
console.log(
'Notification caused app to open from background state:',
remoteMessage.notification,
);
// setTimeout(() => {
// NavigationService.navigate('Menu')
// }, 1200)
// navigation to specific screen
// if (!!remoteMessage?.data && remoteMessage?.data?.redirect_to == "Menu") {
// setTimeout(() => {
// NavigationService.navigate("Menu", { data: remoteMessage?.data })
// }, 1200)
// }
});
// Check whether an initial notification is available
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage.notification,
);
}
});
return unsubscribe;
}
===========这是我在index.js中调用的后台处理程序
messaging().setBackgroundMessageHandler(async remoteMessage => {
await onDisplayNotification(remoteMessage);
console.log('Message handled in the background!', remoteMessage);
});
前台模式下,notifee工作正常。但在后台和安静模式中,出现两个通知(一个来自Firebase,另一个来自NOTIFEE)。
我发送通知的格式:
{
"registration_ids":[
device-tokens......
],
"notification":{
"body":"Test notificaton body",
"title":"Test notification title!"
},
"data": {
"channel_id": "demo-channel",
"channel_name": "demo-channel",
"sound_name": "default"
}
}
1条答案
按热度按时间slhcrj9b1#
Firebase关于消息类型的文档是这样说的:
使用FCM,您可以向客户端发送两种类型的消息:
因此,当应用处于后台状态时收到消息,并且消息具有
notification
属性时,系统将显示该通知。无法将代码配置为显示此通知。如果您希望始终自己处理消息的显示,则应该不发送
notification
消息,而只发送data
消息。当然,您可以将相同的值放入data
消息中,以便代码可以从中获取它们。