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

Node.js中FCM发送通知报错:需指定topic/token/condition之一

问题

编写了如下sendNotif异步函数,使用firebase-admin的messaging.send方法发送FCM通知,函数内已指定有效token,且已完成firebase-admin初始化,但仍收到错误:errorInfo.code为'messaging/invalid-payload',提示'Exactly one of topic, token or condition is required'。

函数代码:

async function sendNotif(title, body0, token) {
  return await admin.messaging().send({
    message: {
      token:
        "dHUKMkIxRbS3uIpdnA1Qef:APA91bHQd2XUpFyWzfdbKrpPV2T9b0uJx9TfKZcyF-O_oAbQ13yA5R-52t_RTb_QSPrMpxw1OV9z8sNFRth5wGuCAld_9VsKr4oRdSWsMzqhrbKcTLC2rAp5QLOUALqiTadyvyvcjTmb!",
      notification: {
        title: title,
        body: body0,
      },
      data: {
        hello: "world",
        click_action: "FLUTTER_NOTIFICATION_CLICK",
      },
      // Set Android priority to "high"
      android: {
        priority: "high",
      },
      // Add APNS (Apple) config
      apns: {
        payload: {
          aps: {
            contentAvailable: true,
          },
        },
        headers: {
          "apns-push-type": "background",
          "apns-priority": "5", // Must be `5` when `contentAvailable` is set to true.
          "apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
        },
      },
    },
  });
}

错误信息:

errorInfo: {
  code: 'messaging/invalid-payload',
  message: 'Exactly one of topic, token or condition is required'
},
解决方法

问题出在admin.messaging().send()的参数结构上:你把整个消息对象包裹在了外层的message键里,但firebase-admin的send方法不需要这个外层的message字段,直接传入消息内容对象即可。

修正后的代码:

async function sendNotif(title, body0, token) {
  return await admin.messaging().send({
    token:
      "dHUKMkIxRbS3uIpdnA1Qef:APA91bHQd2XUpFyWzfdbKrpPV2T9b0uJx9TfKZcyF-O_oAbQ13yA5R-52t_RTb_QSPrMpxw1OV9z8sNFRth5wGuCAld_9VsKr4oRdSWsMzqhrbKcTLC2rAp5QLOUALqiTadyvyvcjTmb!",
    notification: {
      title: title,
      body: body0,
    },
    data: {
      hello: "world",
      click_action: "FLUTTER_NOTIFICATION_CLICK",
    },
    // Set Android priority to "high"
    android: {
      priority: "high",
    },
    // Add APNS (Apple) config
    apns: {
      payload: {
        aps: {
          contentAvailable: true,
        },
      },
      headers: {
        "apns-push-type": "background",
        "apns-priority": "5", // Must be `5` when `contentAvailable` is set to true.
        "apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
      },
    },
  });
}

简单说就是去掉外层的message: {...}结构,直接把token、notification等字段作为参数对象的顶层属性传入send方法,这样firebase就能正确识别到token参数,不会再报缺少目标标识的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 06:39:38