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

Node.js中如何使用FCM向多个Token发送通知?

FCM批量推送通知实现方案

你提到的registration_ids确实已被Firebase Admin SDK弃用,当前批量推送需要使用官方推荐的sendEachForMulticast或sendEach方法,以下是具体实现:

1. 批量发送相同内容给多个Token(推荐)

使用sendEachForMulticast方法,适合给一组Token发送完全相同的通知内容,一次最多支持500个Token:

// 构造批量消息
const multicastMessage = {
  notification: { title: 'Titolo', body: 'Body' },
  tokens: ['token1', 'token2', 'token3'] // 替换为你的目标Token数组
};

// 发送批量通知
Admin.messaging().sendEachForMulticast(multicastMessage)
  .then((response) => {
    console.log(`${response.successCount} 条通知发送成功`);
    console.log(`${response.failureCount} 条通知发送失败`);
    // 可逐个查看失败的Token及原因
    response.responses.forEach((result, index) => {
      if (result.error) {
        console.log(`Token ${multicastMessage.tokens[index]} 发送失败: ${result.error}`);
      }
    });
  })
  .catch((error) => {
    console.error('批量推送出错:', error);
  });

2. 给不同Token发送不同内容

如果需要给每个Token定制不同的通知内容,使用sendEach方法,构造包含多个单条消息的数组:

// 构造多条自定义消息
const messages = [
  {
    notification: { title: '专属通知1', body: '仅你可见的内容1' },
    token: 'token1'
  },
  {
    notification: { title: '专属通知2', body: '仅你可见的内容2' },
    token: 'token2'
  }
];

// 发送多条通知
Admin.messaging().sendEach(messages)
  .then((response) => {
    console.log(`${response.successCount} 条通知发送成功`);
    console.log(`${response.failureCount} 条通知发送失败`);
  })
  .catch((error) => {
    console.error('推送出错:', error);
  });

这两个方法都是FCM当前支持的合法批量推送方式,完全符合token/topics/condition的参数规范,且能返回详细的发送结果,方便后续处理失败的推送请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:05:04