Reputation: 209
I'm trying FCM for the first time, so just using their sample code. In fact I'm even sending their sample messages. The following code, which is straight from the documentation (except for the token which comes from their sample messaging android tool), fails:
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);
});
}
So as you can see the notification would be sent from a cloud function when a document is created. The function is called OK, but the log shows this:
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' }
Upvotes: 4
Views: 7807
Reputation: 1065
I had "
in my strings in my json, I changed it to '
and it fixed the problem!
$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!"
]
]
]
]);
to:
$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!'
]
]
]
]);
Hope it helps!
Upvotes: 0
Reputation: 3452
As Carlos Fernandez Sanz pointed out, one cause for this is that the client and server are connected to different firebase projects. Project name appears in the google-services.json
file on the client and in the credentials json on the server.
Upvotes: 1