首页 > 解决方案 > Flutter 和 FCM 发送重复通知 (android)

问题描述

在我的聊天应用程序中,我将消息存储在 firestore 文档中,因此我仅使用 FCM 来通知用户有关文档中的新消息。我将所有聊天参与者令牌存储在一个字段中,并通过 for 循环向所有参与者发送消息。

                   for (var d in widget.currMatch.deviceTokens) {
                      
                      if (d != thisDeviceToken) // I don't send the notification to this device
                        Api().sendFcm(
                            'New Message',
                            'you have a new message in your chat',
                            d);
                    }

如果应用程序关闭完全没有问题,我会在系统托盘中收到通知,没关系。

但是在应用程序打开的情况下,我会收到有时重复 3 次有时 5 次的通知(这似乎是随机的,但谁知道)。我使用快餐栏来显示通知,所以我看到它出现和消失了 3-5 次。

这个问题不会出现在安卓模拟器上,而只会出现在物理设备上。

这是接收器部分的代码

_listener = FirebaseMessaging.onMessage.listen((RemoteMessage event) {
      print("message received");
      print(event.notification.body);

      // fa vedere una snackbar quando arriva un nuovo messaggio da FCM
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(event.notification.body),
          duration: const Duration(seconds: 2),
          action: SnackBarAction(
            label: 'OK',
            onPressed: () {},
          ),
        ),
      );

这是为什么?

标签: androidfirebaseflutterfirebase-cloud-messaging

解决方案


推荐阅读