Firebase FCM : invalid-argument

标签 firebase firebase-cloud-messaging google-cloud-functions

我是第一次尝试 FCM,所以只需使用他们的示例代码。事实上,我什至在发送他们的示例消息。以下代码直接来自文档(来自其示例消息传递 android 工具的 token 除外)失败:

exports.onBroadcastCreated =    functions.firestore.document('/apath    /...').onCreate(async event => {
   notification:{
  title:"Portugal vs. Denmark",
  body:"great match!"
},
token: 'eU2YUsi4Ugs:APA91bFH5bR9B1xosqrjvpw7HG4UkYTlDizmtra9pQRge-b4JxRbLjq9PVw91rqZytkUMKJXjPHd_dRlHHMk1bExCo_6Dxv99Vfp8MYz-H16Y9zmG8EFlWXNH4Tw_h6NRj2z1gLcz10m'
};

// Send a message to the device corresponding to the provided
// registration token.
return 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);
    console.log(message);
  });

}

如您所见,当创建文档时,通知将从云功能发送。该函数被称为 OK,但日志显示如下:
Error sending message: { Error: Request contains an invalid argument.
    at FirebaseMessagingError.Error (native)
    at FirebaseMessagingError.FirebaseError [as constructor] (/user_code   /node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
    at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16)
    at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:149:50
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
  errorInfo: 
   { code: 'messaging/invalid-argument',
     message: 'Request contains an invalid argument.' },
  codePrefix: 'messaging' }

{ notification: { title: 'Portugal vs. Denmark', body: 'great match!' },
  token: 'eU2YUsi4Ugs:APA91bFH5bR9B1xosqrjvpw7HG4UkYTlDizmtra9pQRge-b4JxRbLjq9PVw91rqZytkUMKJXjPHd_dRlHHMk1bExCo_6Dxv99Vfp8MYz-H16Y9zmG8EFlWXNH4Tw_h6NRj2z1gLcz10m' }

最佳答案

正如 Carlos Fernandez Sanz 指出的那样,造成这种情况的一个原因是客户端和服务器连接到不同的 Firebase 项目。项目名称出现在 google-services.json 中文件在客户端和服务器上的凭据 json 中。

关于Firebase FCM : invalid-argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50420582/

相关文章:

firebase - Angular 5 + Firebase : Namespace 'firebase.auth' has no exported member 'IdTokenResult'

google-cloud-platform - Google Cloud 后台功能是否有最大超时时间?

javascript - 创建新用户时如何将数据写入firestore?

javascript - 如何在 Firebase 的 Angular-Fire 中使用 $save() 来更新对象属性值

javascript - Firebase 防火 promise 抛出错误

angular - 如何读取 firebase 推送通知内容并触发 ionic2 中的方法?

firebase - 如何对用express包装的firebase函数进行单元测试?

javascript - 使用对象元素作为参数的 Firestore 查询

android - 添加firebase_messaging插件后,Gradle构建失败

ios - 推送通知已发送但 didReceiveRemoteNotification 从未被称为 Swift