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

Node.js集成FCM推送通知遇MismatchSenderId错误求助

解决FCM推送的MismatchSenderId错误

我来帮你搞定这个头疼的MismatchSenderId问题!这个错误本质就是你的服务器端使用的发送方信息,和客户端生成Token时绑定的Firebase项目不匹配,咱们一步步排查解决:

1. 核心排查:Server Key与客户端Token必须属于同一个Firebase项目

这是最常见的原因,你得仔细核对:

  • 打开Firebase控制台,找到你Android客户端对应的项目,进入「Cloud Messaging」页面,复制这里的Server Key(注意是当前项目的,别拿成其他项目的了)。
  • 确认你代码里的serverKey变量,就是这个项目的Server Key,没有复制错误或者多了空格。
  • 同时检查Android客户端的google-services.json文件,必须是从同一个Firebase项目下载的——这个文件里的project_number就是Sender ID,客户端生成的Token会和这个Sender ID绑定,一旦Server Key对应的项目和这个Sender ID不匹配,就会触发MismatchSenderId错误。

2. 尝试切换到官方推荐的firebase-admin SDK

你用的fcm-node和fcm-push都是第三方包,不如官方的firebase-admin SDK稳定可靠,而且能直接通过服务账号绑定项目,减少密钥不匹配的风险。给你个示例代码:

const admin = require('firebase-admin');
const serviceAccount = require('./path/to/your/service-account-key.json'); // 从Firebase项目设置里下载

// 初始化Admin SDK
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://your-firebase-project-id.firebaseio.com' // 替换成你的项目数据库URL
});

router.post('/pushmessage', async (req, res) => {
  try {
    const { userToken } = req.body;
    const message = {
      notification: { title: 'hello', body: 'test' },
      data: { my_key: 'my value', contents: "abcv/" },
      token: userToken
    };

    const response = await admin.messaging().send(message);
    console.log("MESSAGE SEND");
    res.json({ status: 1, message: response });
  } catch (err) {
    console.log("error : "+err);
    res.json({ status: 0, message: err.message });
  }
});

3. 用curl直接测试,排除代码包的问题

如果还是不确定,可以用curl直接发送请求,验证Server Key和Token是否匹配:

curl -X POST "https://fcm.googleapis.com/fcm/send" \
-H "Authorization: key=YOUR_SERVER_KEY" \
-H "Content-Type: application/json" \
-d '{
  "to": "YOUR_CLIENT_TOKEN",
  "notification": {
    "title": "Test",
    "body": "Test message"
  }
}'

如果这个请求也返回MismatchSenderId,那100%是Server Key和Token的项目不对应,再回去仔细核对Firebase项目;如果请求成功,那就是你用的第三方包有问题,直接换firebase-admin就好。

最后提醒

别忽略一些细节:比如复制Server Key的时候有没有漏字符,客户端有没有在多个Firebase项目之间切换过导致Token残留,这些小问题都可能引发错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:39:02