使用Node.js发送Firebase推送通知遇SenderId mismatch错误求助
解决Firebase推送通知的SenderId mismatch错误
我的代码(app.js)
var admin = require("firebase-admin"); var serviceAccount = require("./serviceAccountKey.json"); console.log(serviceAccount); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://my_project_id-default-rtdb.asia-southeast1.firebasedatabase.app" }); const message = { notification: { title: 'Hello from Firebase!', body: 'This is a push notification from Firebase.' }, token: 'ekBu_n:APA91bEAXiyJvEbk5Vb4u7tJgCjFXGHL6sNypCgYTP3qPfy1nC2Sw9Q7H6zRDo8-AM31l6GI-twJe72t4z4k-hGy1KNgtpsWaXeP2tieH_J7XyZYDr5LWevh5g2TnOhEgNafnW0t-o0d' // Replace with the specific device token }; // Send the message admin.messaging().send(message) .then((response) => { console.log('Notification sent successfully:', response); }) .catch((error) => { console.error('Error sending notification:', error); });
运行时错误
Error sending notification: FirebaseMessagingError: SenderId mismatch at Function.fromServerError (D:\firebase_notification_demo\node_modules\firebase-admin\lib\utils\error.js:254:16) at createFirebaseError (D:\firebase_notification_demo\node_modules\firebase-admin\lib\messaging\messaging-errors-internal.js:35:47) at D:\firebase_notification_demo\node_modules\firebase-admin\lib\messaging\messaging-api-request-internal.js:79:75 at processTicksAndRejections (node:internal/process/task_queues:96:5) { errorInfo: { code: 'messaging/mismatched-credential', message: 'SenderId mismatch' }, codePrefix: 'messaging' }
解决方法
这个错误核心是后台使用的Firebase项目SenderId,和设备令牌(FCM Token)所属项目的SenderId不匹配,按以下步骤排查:
核对项目一致性
检查客户端生成FCM Token时用的Firebase项目(对应google-services.json/GoogleService-Info.plist文件),和后台serviceAccountKey.json对应的Firebase项目是否为同一个。如果不是,切换到同一个项目。确认SenderId匹配
打开Firebase控制台,进入后台使用的项目,在项目设置→云消息传递里查看SenderId;再打开客户端配置文件,找到sender_id字段,确保两者完全一致。重新生成正确的FCM Token
如果项目不匹配,在客户端替换为后台对应项目的配置文件,重新获取FCM Token,替换代码里的token值。检查服务账号文件
确保serviceAccountKey.json是从正确的Firebase项目下载的,不要混用其他项目的密钥文件。
内容的提问来源于stack exchange,提问作者Alagusundaram R
相关产品推荐
相关产品推荐

