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

如何禁用FCM自带通知,避免与AwesomeNotifications重复显示?

解决FCM与AwesomeNotifications通知重复显示问题

问题说明

使用Firebase Cloud Messaging(FCM)作为推送服务,搭配AwesomeNotifications处理角标和点击逻辑时,出现通知重复显示的问题:一条由FCM自动弹出,另一条由AwesomeNotifications触发。需要禁用FCM自带的通知显示,仅保留AwesomeNotifications的通知。

解决方案

1. 禁用FCM前台自动弹出通知

修改FCM的前台通知展示配置,关闭FCM自动弹出通知的能力,只让AwesomeNotifications负责前台通知展示:

修改_setupFcmNotificationSettings方法中的setForegroundNotificationPresentationOptions配置:

static Future<void> _setupFcmNotificationSettings() async {
  // 关闭FCM前台自动弹出通知,按需保留声音/角标配置
  fcm.setForegroundNotificationPresentationOptions(
    alert: false, // 核心:禁用FCM前台弹出通知
    sound: true, // 可根据需求保留或关闭
    badge: false, // 若角标由AwesomeNotifications处理,设为false
  );

  await fcm.requestPermission(
    alert: true, // 权限仍需正常请求,仅阻止FCM自行弹通知
    badge: true,
    sound: true,
    provisional: true,
  );
}

2. 确保后台消息为「数据消息」

FCM的消息分为两种类型:

  • 通知消息:包含notification字段,FCM后台会自动弹出通知,不受onBackgroundMessage控制
  • 数据消息:仅包含data字段,会完全交给onBackgroundMessage处理,不会自动弹通知

要避免后台重复通知,需让后端发送仅含data字段的数据消息,示例请求体:

{
  "to": "目标设备Token",
  "data": {
    "title": "通知标题",
    "body": "通知内容",
    "to": "/跳转路由"
  }
}

这样后台消息会触发_fcmBackgroundHandler,由AwesomeNotifications统一弹出通知,不会出现FCM自动弹通知的情况。

额外说明

如果无法修改后端消息格式(必须发送含notification字段的通知消息),可在AndroidManifest.xml中添加配置,阻止FCM后台自动弹通知:

<meta-data
    android:name="firebase.messaging.default_notification_channel_id"
    android:value="@string/empty_channel_id" />

同时在strings.xml中定义一个空的渠道ID:

<string name="empty_channel_id">empty_channel</string>

此方法仅针对Android生效,优先推荐使用「数据消息」方案。

最终效果

修改完成后,前台和后台的通知都会由AwesomeNotifications统一处理,FCM不再自动弹出通知,彻底解决重复显示问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 09:52:51