Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google FCM Invalid_argument when sending message

I'm trying to figure out how to do push notifications to Android using Firebase and got it working using the legacy HTTP (https://fcm.googleapis.com/fcm/send) but the documentation suggests using the newer endpoint (https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send). I can't seem to get it working because I keep getting this response:

{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT"
    }
}

This occurs using the sample at https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices:

POST https://fcm.googleapis.com/v1/projects/project-916177026973/messages:send HTTP/1.1
cache-control: no-cache
Postman-Token: 81403929-77ba-4568-8681-a854527ccb22
Content-Type: application/json
Authorization: Bearer <token redacted>
User-Agent: PostmanRuntime/6.4.1
Accept: */*
Host: fcm.googleapis.com
accept-encoding: gzip, deflate
content-length: 319
Connection: close

{
  "message":{
    "token" : <token redacted>,
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message",
      }
   }
}

I also tried with the last comma removed to make it compliant with JSON and still no luck. Any ideas?

like image 359
Wade Tracy Avatar asked Oct 17 '22 02:10

Wade Tracy


1 Answers

As the OP pointed out Firebase now suggest to the FCM v1 endpoint if you want to send messages via HTTP.

To send messages via HTTP, send an HTTP POST request to the FCM v1 endpoint and specify the send method. The endpoint URL must contain the project ID of the Firebase project for your app, available in the General project settings tab of the Firebase console.

It will look something like this:

POST https://fcm.googleapis.com/v1/projects/your_project_id_here/messages:send HTTP/

The section of the URL your_project_id_here is your project id. You will need to find the project specific project Id which is located in the Settings section of the Firebase console for your project under the tab General.

Look for Project ID. If you have any doubts about your correct Project ID it is also a part if the URL in the website address to your project. It looks like:

https://console.firebase.google.com/project/your_project_id_here/settings/general/android:your_project_name
like image 125
Barns Avatar answered Oct 20 '22 06:10

Barns