FCM Node JS Server,

时间:2018-05-14 21:33:02

标签: javascript node.js firebase firebase-cloud-messaging

您正在使用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);
  });
}

但是当我执行时,显示此错误:

  

发送消息时出错:{错误:请求包含无效参数。       在FirebaseMessagingError.FirebaseError [作为构造函数](C:\ Users \ Daniel \ Documents \ Monitora \ Demos \ BackEnd \ APIMonitora \ node_modules \ firebase-admin \ lib \ utils \ error.js:39:28)       在FirebaseMessagingError.PrefixedFirebaseError [作为构造函数](C:\ Users \ Daniel \ Documents \ Monitora \ Demos \ BackEnd \ APIMonitora \ node_modules \ firebase-admin \ lib \ utils \ error.js:85:28)       在新的FirebaseMessagingError(C:\ Users \ Daniel \ Documents \ Monitora \ Demos \ BackEnd \ APIMonitora \ node_modules \ firebase-admin \ lib \ utils \ error.js:241:16)       在Function.FirebaseMessagingError.fromServerError(C:\ Users \ Daniel \ Documents \ Monitora \ Demos \ BackEnd \ APIMonitora \ node_modules \ firebase-admin \ lib \ utils \ error.js:271:16)       在C:\ Users \ Daniel \ Documents \ Monitora \ Demos \ BackEnd \ APIMonitora \ node_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',        消息:'请求包含无效参数。' },codePrefix:' messaging' }

SDK加载,依赖是可行的。 你能帮我解决一下吗?

1 个答案:

答案 0 :(得分:0)

抱歉我的错误, 我的数据库上的字符串限制不存储修复此问题的所有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);
  });
}