当后台 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 包也会在通知标志中包含标题和正文 有什么方法可以使通知仅由 flutter_local_notification 包处理
flutter_local_notifications
函数之前初始化 _showNotification
包和 android 通道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,
);
您可以在这里找到更多信息
对我来说,有效的是将空映射传递到 firebase 发送的消息中的通知字段,因为标题和正文都为空,所以它不会触发 SDK 显示通知:
`notification : {},`
然后通过传递给 flutter 本地通知包的消息数据显示我的通知
如图
{ "message":{ "token":"token_1", "data":{"title":"FCM Message", "body":"This is an FCM notification message!" }, "notification":{} } }
完全省略通知会导致 iOS 在应用程序终止时不显示推送通知,并将其包含在“body”和“title”字段中会导致 SDK 推送通知各显示两次并抖动本地通知,希望这会有所帮助。干杯