4

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' }
  • 1
    Are you confident the token is valid? It is typically obtained from one of the client SDKs. For example, on Android, the method is [FirebaseInstanceId.getInstance().getToken()](https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token). The docs for [getting the token on iOS are here](https://firebase.google.com/docs/cloud-messaging/ios/client#receive-the-current-registration-token). – Bob Snyder May 19 '18 at 01:09
  • @BobSnyder Yes, it was an issue with the token. I was getting desperate so I tried the deprecated API, which is the same thing except that the registration token is passed as an additional parameter to sendToDevice() instead of as part of the message passed to send(). Fortunately this deprecated API provides a useful diagnostics - specifically, the token was generated by a different firebase project than the one trying to send a message. No idea how that happened but it's fixed now, so moving on. Thanks! – Carlos Fernandez Sanz May 21 '18 at 18:19
  • Thanks, @CarlosFernandezSanz, we wasted a day on the server side having a private key json for a different project until your comment saved us! – Aur Saraf Jun 28 '18 at 08:45

2 Answers2

1

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.

Aur Saraf
  • 3,214
  • 1
  • 26
  • 15
0

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!

Mohamad Eghlima
  • 970
  • 10
  • 23