我第一次尝试使用FCM,所以只使用他们的示例代码。事实上,我甚至发送他们的示例消息。以下代码直接来自文档(除了来自他们的示例消息传递android工具的令牌),它失败了:
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' }
答案 0 :(得分:1)
正如Carlos Fernandez Sanz所指出的,其原因之一是客户端和服务器连接到了不同的Firebase项目。项目名称显示在客户端上的google-services.json
文件中和服务器上的凭据json中。
答案 1 :(得分:0)
我的json字符串中有"
,我将其更改为'
并解决了问题!
$response = $client->post(
'https://fcm.googleapis.com/v1/projects/xxx/messages:send',[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token['access_token'],
],
GuzzleHttp\RequestOptions::JSON => [
"message" => [
"token" => "dflhjldkjhflksfshklsf",
"notification" => [
"title" => "FCM Message",
"body" => "This is an FCM notification message!"
]
]
]
]);
收件人:
$response = $client->post(
'https://fcm.googleapis.com/v1/projects/xxx/messages:send',[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token['access_token'],
],
GuzzleHttp\RequestOptions::JSON => [
'message' => [
'token' => 'dflhjldkjhflksfshklsf',
'notification' => [
'title' => 'FCM Message',
'body' => 'This is an FCM notification message!'
]
]
]
]);
希望有帮助!