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

使用NodeJS Admin SDK发送FCM通知设备无法接收问题求助

问题原因

  • 你的消息结构仅携带data字段,属于静默数据消息,Android/iOS系统默认不会为这类消息自动弹出通知栏提示,只有应用在前台运行时由你自行编码接收处理才会触发对应逻辑。你通过Notification composer、Postman发送的消息默认携带notification字段(包含标题、正文等通知配置),所以设备能正常展示通知。
  • 你将res.end()放在了异步发送逻辑的外部,虽然当前测试环境下控制台显示发送成功,但生产环境大概率会出现函数提前终止、消息发送失败的问题。

修复方案

调整消息结构增加notification字段,同时修正异步流程的执行顺序,修正后代码如下:

exports.sendNotification = functions.https.onRequest((req, res) => {
  const message = {
    notification: {
      title: "天气预警",
      body: "新的天气预警已发布!"
    },
    data: {
      type: "warning",
      content: "A new weather warning has been created!",
    },
    topic: "weather",
  };
  
  admin
    .messaging()
    .send(message)
    .then((response) => {
      console.log("Successfully sent message:", response);
      res.status(200).send("通知发送成功");
    })
    .catch((error) => {
      console.log("Error sending message:", error);
      res.status(500).send("通知发送失败:" + error.message);
    });
});

如果你的业务确实需要仅发送纯data类型的消息,请在移动端代码中补充后台消息监听逻辑,主动调用系统通知能力弹框,否则系统不会自动展示这类消息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 16:45:03