当应用程序处于 Background/Terminated 并且收到 FCM 通知时,如何在 Flutter 中显示本地通知?
How to show a Local Notification in Flutter, when the app is in Background/Terminated and it receives an FCM notification?
我正在尝试在我的 Flutter 应用程序中使用 Firebase 云消息传递实现推送通知服务。这就是 FCM 文档所说的我们应该根据应用程序的当前状态(前台、后台或已终止)处理通知的方式:
前景:我们有一个 onMessage
流,我们可以收听,但 FCM 不会 显示任何通知当处于前台状态时,所以我们必须为此使用 FlutterLocalNotificationsPlugin
,这是我的实现:
FirebaseMessaging.onMessage.listen((RemoteMessage remoteMessage) {
// calling flutter local notifications plugin for showing local notification
scheduleNotification(remoteMessage);
});
在 scheduleNotification
方法中我调用了 FlutterLocalNotificationsPlugin().show()
方法,直到现在一切都按预期工作。
问题从这里开始:
后台,已终止:Firebase 在此状态下自动显示通知,它有一个 onBackgroundMessage. method, to which we can pass an BackgroundMessageHandler 在 FCM 之后运行已显示通知。这是我对后台处理程序的实现:
Future<void> backgroundMessageHandler(
RemoteMessage remoteMessage) async {
RemoteNotification? remoteNotification = remoteMessage.notification;
if (remoteNotification != null) {
FlutterLocalNotificationsPlugin().show(
remoteMessage.messageId.hashCode,
remoteNotification.title,
remoteNotification.body,
NotificationDetails(
android: AndroidNotificationDetails(
_androidNotificationChannel.id,
_androidNotificationChannel.name,
_androidNotificationChannel.description,
icon: 'launch_background',
importance: Importance.max),
));
}
}
问题:每次我的应用程序收到来自 FCM 的通知时,我的应用程序都会显示 两个 条通知,其中一个由 FCM 自动显示,第二个由我在 BackgroundMessageHandler
.
中调用的 FlutterLocalNotificationsPlugin().show()
方法显示
Tl:dr
如何防止 FCM 自动显示任何通知并仅通过 FlutterLocalNotificationsPlugin().show()
方法显示?
一个解决方案是,我不从 FCM 发送通知,只发送数据消息,FCM 不显示任何通知。但是,我不认为这是正确的方法。
从上面的代码中删除 FlutterLocalNotificationsPlugin().show
调用。您不需要手动显示通知。
如您所说,此代码为 运行 时已显示通知。
还是我遗漏了什么?
我在这里回答我自己的问题,
经过一些研究,我在 FCM 文档中发现,如果我们想处理在客户端显示通知,它确实提到使用仅数据消息。
我们可以阅读它 here
Client app is responsible for processing data messages. Data messages have only custom key-value pairs with no reserved key names (see below).
Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.
这个东西在flutter fire docs,
中也有提到
Your application code can then handle messages as you see fit; updating local cache, displaying a notification or updating UI. The possibilities are endless!
这就是我猜的答案,我只是在传递 FCM 消息时不必使用“通知”字段,客户端上的 FCM 插件不会自动显示通知。我仍然认为这应该在文档中说得更清楚,引起了很多混乱和研究,我仍然认为它有点奇怪,当我们打算向用户显示通知时,但我们仍然省略了“通知”字段.
一开始我遇到了同样的问题作为解决方案我省略了json中的通知发送但是在尝试和测试之后,我发现我可以发送通知和数据而不会出现双推送通知问题,解决了。
解决方案:检查您的 onbackgroundhandler 并验证数据
if (message.data.message ['android_channel_id'] == my channel) {
flutterLocalNotificationsPlugin.show();
}
我正在尝试在我的 Flutter 应用程序中使用 Firebase 云消息传递实现推送通知服务。这就是 FCM 文档所说的我们应该根据应用程序的当前状态(前台、后台或已终止)处理通知的方式:
前景:我们有一个 onMessage
流,我们可以收听,但 FCM 不会 显示任何通知当处于前台状态时,所以我们必须为此使用 FlutterLocalNotificationsPlugin
,这是我的实现:
FirebaseMessaging.onMessage.listen((RemoteMessage remoteMessage) {
// calling flutter local notifications plugin for showing local notification
scheduleNotification(remoteMessage);
});
在 scheduleNotification
方法中我调用了 FlutterLocalNotificationsPlugin().show()
方法,直到现在一切都按预期工作。
问题从这里开始:
后台,已终止:Firebase 在此状态下自动显示通知,它有一个 onBackgroundMessage. method, to which we can pass an BackgroundMessageHandler 在 FCM 之后运行已显示通知。这是我对后台处理程序的实现:
Future<void> backgroundMessageHandler(
RemoteMessage remoteMessage) async {
RemoteNotification? remoteNotification = remoteMessage.notification;
if (remoteNotification != null) {
FlutterLocalNotificationsPlugin().show(
remoteMessage.messageId.hashCode,
remoteNotification.title,
remoteNotification.body,
NotificationDetails(
android: AndroidNotificationDetails(
_androidNotificationChannel.id,
_androidNotificationChannel.name,
_androidNotificationChannel.description,
icon: 'launch_background',
importance: Importance.max),
));
}
}
问题:每次我的应用程序收到来自 FCM 的通知时,我的应用程序都会显示 两个 条通知,其中一个由 FCM 自动显示,第二个由我在 BackgroundMessageHandler
.
FlutterLocalNotificationsPlugin().show()
方法显示
Tl:dr
如何防止 FCM 自动显示任何通知并仅通过 FlutterLocalNotificationsPlugin().show()
方法显示?
一个解决方案是,我不从 FCM 发送通知,只发送数据消息,FCM 不显示任何通知。但是,我不认为这是正确的方法。
从上面的代码中删除 FlutterLocalNotificationsPlugin().show
调用。您不需要手动显示通知。
如您所说,此代码为 运行 时已显示通知。
还是我遗漏了什么?
我在这里回答我自己的问题, 经过一些研究,我在 FCM 文档中发现,如果我们想处理在客户端显示通知,它确实提到使用仅数据消息。 我们可以阅读它 here
Client app is responsible for processing data messages. Data messages have only custom key-value pairs with no reserved key names (see below).
Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.
这个东西在flutter fire docs,
中也有提到Your application code can then handle messages as you see fit; updating local cache, displaying a notification or updating UI. The possibilities are endless!
这就是我猜的答案,我只是在传递 FCM 消息时不必使用“通知”字段,客户端上的 FCM 插件不会自动显示通知。我仍然认为这应该在文档中说得更清楚,引起了很多混乱和研究,我仍然认为它有点奇怪,当我们打算向用户显示通知时,但我们仍然省略了“通知”字段.
一开始我遇到了同样的问题作为解决方案我省略了json中的通知发送但是在尝试和测试之后,我发现我可以发送通知和数据而不会出现双推送通知问题,解决了。
解决方案:检查您的 onbackgroundhandler 并验证数据
if (message.data.message ['android_channel_id'] == my channel) {
flutterLocalNotificationsPlugin.show();
}