You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中FirebaseMessaging的onMessage与onMessageOpenedApp重复触发问题

问题原因及解决方案

你的问题出在代码逻辑上,并非Firebase的已知问题。当应用处于关闭状态时,Firebase会通过系统通道展示通知;当你点击这条通知打开应用时,FirebaseMessaging.onMessageOpenedApp会接收到触发打开的消息,但你在这个监听里又调用了NotificationService().showNotification(message),这就导致了通知被重复触发。

修复代码

直接移除onMessageOpenedApp监听中的通知显示逻辑即可,如果你需要在点击通知后执行业务操作(比如跳转指定页面),只保留对应的业务代码:

@override
void initState() {
  super.initState();

  _initFuture = initData();
  messaging = FirebaseMessaging.instance;
  _requestNotificationPermissions();

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      NotificationService().showNotification(message);
  });

  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      // 这里只处理点击通知后的业务逻辑,比如跳转页面
      // Navigator.push(context, MaterialPageRoute(builder: (_) => TargetPage()));
  });

}

补充说明

你之前尝试的校验messageId方案之所以没成功,大概率是因为没必要——问题的核心是重复调用了通知显示方法,而非消息重复接收。如果后续遇到真的重复接收消息的场景,可以将已处理的messageId持久化存储(比如用SharedPreferences),每次接收消息时先校验是否已存在,再决定是否处理。

内容的提问来源于stack exchange,提问作者Icaroto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:12:35