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

FCM通知添加tag字段报错,如何正确使用tag?

问题分析与解决方案

问题根源

你错误地将tag字段放在了FCM消息的notification对象中,但FCM的通用notification payload结构并不包含tag字段。tag是Android平台专属的通知配置项,用于控制通知的覆盖或分组,必须放在平台特定的配置节点下。

正确的代码实现

将tag字段移到android.notification对象中即可解决报错。如果需要iOS端实现类似的通知分组功能,可以同时配置apns.payload.aps.threadId:

exports.testNotification = functions.https.onCall(async (data, context) => {
  if (data != null) {
    const message = {
      token: '<fcm_token>',
      notification: {
        title: 'Dummy title',
        body: 'Dummy body',
      },
      // Android平台专属配置:tag用于通知覆盖/分组
      android: {
        notification: {
          tag: 'foo',
        },
      },
      // iOS平台类似功能:threadId用于通知分组
      apns: {
        payload: {
          aps: {
            threadId: 'foo',
          },
        },
      },
    };

    try {
      const response = await admin.messaging().send(message);
      console.log('消息发送成功:', response);
    } catch (error) {
      console.log('发送消息失败:', error);
    }
  }

  return null;
});

补充说明

  • Android端:相同tag值的新通知会替换掉旧通知,适合需要更新通知内容的场景;
  • iOS端:threadId会将同标识的通知归为一组,用户可以折叠查看,实现类似的分组效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:30:43