android - 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: () {},
),
),
);
这是为什么?
解决方案
推荐阅读
- python - 创建图像表面窗口的精灵表
- c# - 将 JSON 作为对象的一部分传递给 .net 核心 API
- java - 避免基本身份验证的不必要的 Keycloak 会话
- python - ValueError:检查目标时出错:预期dense_24具有形状(1,)但得到形状为(77,)的数组
- html - 在不使用 flask-bootstrap 的情况下使用 wtfroms
- javascript - Javascript - .append 方法有问题
- excel - VBA:根据另一张纸的输入从一张纸中选择多个值
- javascript - React - 通过状态更新管理条件渲染
- mysql - 为什么我不能将变量传递给 MySQL 中的 VARCHAR(str_len)?
- javascript - 反应路由器不适用于现场生产