嗨,我在使用 Node JS 和 Firebase SDK,
我试图发送 FCM 通知,因为文档说:https://firebase.google.com/docs/cloud-messaging/admin/send-messages?hl=es-419,
我的代码:
function testMessage(){
let token="f8mNlJq4VOY:APA91bF5_8ldOZEm34ajCfNx7hZ9_LhjUBQFDwZbtSnCNEzb1bEtMsXKlM8upyicvmnJ92xELZzDSxTMaeZrCrrau"
let message={
notification: {
title: "Account Deposit",
body: "A deposit to your savings account has just cleared."
},
data: {
score: '850',
time: '2:45'
},
token:token
};
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
}
但是当我执行时,这会显示此错误:
发送消息时出错:{ 错误:请求包含无效参数。 at FirebaseMessagingError.FirebaseError [as constructor] (C:\Users\Daniel\Documents\Monitora\Demos\BackEnd\APIMonitoraode_modules\firebase-admin\lib\utils\error.js:39:28( at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (C:\Users\Daniel\Documents\Monitora\Demos\BackEnd\APIMonitoraode_modules\firebase-admin\lib\utils\error.js:85:28( at new FirebaseMessagingError (C:\Users\Daniel\Documents\Monitora\Demos\BackEnd\APIMonitoraode_modules\firebase-admin\lib\utils\error.js:241:16( at Function.FirebaseMessagingError.fromServerError (C:\Users\Daniel\Documents\Monitora\Demos\BackEnd\APIMonitoraode_modules\firebase-admin\lib\utils\error.js:271:16( at C:\Users\Daniel\Documents\Monitora\Demos\BackEnd\APIMonitoraode_modules\firebase-admin\lib\messaging\messaging-api-request.js:149:50 在 at process._tickCallback (internal/process/next_tick.js:188:7( errorInfo: { code: 'messaging/invalid-argument', 消息:"请求包含无效参数"},代码前缀:"消息传递" }
SDK 它的加载和依赖项都在工作。 你能帮我修复它吗?
对不起,这是我的错误, 我的数据库上的字符串限制不存储解决此问题的所有ID,代码看起来:
function testMessage(){
let registrationToken ='f8mNlJq4VOY:APA91bF5_8ldOZEm34ajCfNx7hZ9_LhjUBQFDwZbtSnCNEzb1bEtMsXKlM8upyicvmnJ92xELZzDSxTMaeZrCrrau-S0mmNdMC18Mp3rwLnUGy-cXM8caoiCJQjEpDDy6FChDwtmO3n2';
let message={
notification: {
title: "Portugal vs. Denmark",
body: "5 to 1"
},
data: {
score: '850',
time: '2:45'
},
token: registrationToken
};
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
}