firebase和flutter本地通知都处理后台通知



当后台firebase中的应用程序首先处理通知,然后flutter本地通知在屏幕上弹出,使firebase不显示通知时,我面临显示双重通知的问题

p.s删除
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler(;

使应用程序不处理后台上的通知

FirebaseMessaging.onMessage.listen(
(RemoteMessage? message) async {

_showNotification(
id: 0,
title: message?.notification?.title ?? '',
body: message?.notification?.body ?? '');
},
);
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

firebaseMessagingBackgroundHandler:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage? message) async {
_showNotification(
id: 0,
title: message?.notification?.title ?? '',
body: message?.notification?.body ?? '');
}

firebase包在通知标志中包含标题和正文,即使应用程序在后台工作使通知仅由flatter_local_notification包处理的任何方法

  • 一个可能的问题是初始化local_notification_package

这应该是最高级的初始化,即在调用_showNotification函数之前初始化flutter_local_notifications包和android通道

  • 另一个可能的问题是firebase的初始化。这应该在应用程序运行之前完成,因为这是一个前台通知

您的firebaseMessagingBackgroundHandler将看起来像

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage? message) async {
await Firebase.initializeApp(); //initialize firebase
//INITIALIZE YOUR FLUTTER LOCAL NOTIFICATION PACKAGE 
// INITIALIZE YOUR ANDROID CHANNEL
//then show the notification
_showNotification(
id: 0,
title: message?.notification?.title ?? '',
body: message?.notification?.body ?? '');
}

在添加之前,我遇到了同样的问题

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />

在android清单文件中。

此外,请记住,值high_importance_channel可以是您想要的任何东西,但它应该与您在flutter代码中声明的值相同,如下所示:

const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', //channel id
'High Importance Notifications', //title
'This channel is used for important notifications.', //description
importance: Importance.max,
);

你可以在这里找到更多信息

最新更新