我是第一次尝试 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/