我是第一次尝试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' }
正如Carlos Fernandez Sanz指出的那样,其中一个原因是客户端和服务器连接到不同的Firebase项目。项目名称显示在客户端上的google-services.json
文件中,以及服务器上的凭据 json 中。
我在 json 中的字符串中"
,我将其更改为'
并解决了问题!
$response = $client->post(
'https://fcm.googleapis.com/v1/projects/xxx/messages:send',[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token['access_token'],
],
GuzzleHttpRequestOptions::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'],
],
GuzzleHttpRequestOptions::JSON => [
'message' => [
'token' => 'dflhjldkjhflksfshklsf',
'notification' => [
'title' => 'FCM Message',
'body' => 'This is an FCM notification message!'
]
]
]
]);
希望对您有所帮助!