Send your test FCM push notification with cURL

Authorization needed to send push notifications with FCM is cool and not so complicated to integrate on your server app. Take a look at documentation here.

Quicker way

Server app? Great, but sometimes you have to quickly test your FCM push notifications and not necessarily you want create a whole back-end infrastructure just to send some notifications out.

So what to do then? Well, you can use cURL or any browser-based HTTP requester such as Postman. But it’s not as easy as it was when FCM was a Google Cloud Messaging

As the GCM service and its API are deprecated from April 2018 you can’t send your messages using API key anymore. It’s based on the OAuth2 right now.

So how can you get your tokens now? Luckily, there’s a Google’s OAuth 2.0 Playground where you can get your tokens for test purposes.

How to play on the playground?
It’s pretty easy.
Type email, https://www.googleapis.com/auth/firebase.messaging into scopes field. Then click on the Authorize APIs button.

Then Google will ask you for permissions. After that you’ll receive your authorization token. You have to exchange it to obtain the proper access token.
Then you’ll see a JSON-formatted response. Copy the value of the access_token field.
Please remember that the token will be valid for the 1 hour only. After this time you have to refresh it. Of course you can do it using the OAuth 2.0 Playground as well.

Send a notification via curl

Finally you can use your token and send a simple push notification to your target device (using device’s token)

curl -X POST -H "Authorization: Bearer YOUR-TOKEN" -H "Content-Type: application/json" -d '{
  "message":{
    "token":"TARGET_DEVICE_TOKEN",
    "notification":{
      "title":"Hello",
      "body":"This is a text message!"
    }
  }
}' https://fcm.googleapis.com/v1/projects/YOUR-PROJECT-SHORT-NAME/messages:send

Or send it to the topic’s subscribers

curl -X POST -H "Authorization: Bearer YOUR-TOKEN" -H "Content-Type: application/json" -d '{
  "message":{
    "topic":"YOUR-TOPIC-NAME",
    "notification":{
      "title":"Hello",
      "body":"This is a text message!"
    }
  }
}' https://fcm.googleapis.com/v1/projects/YOUR-PROJECT-SHORT-NAME/messages:send

To get more detailed info about the request and response you can use –verbose flag.

Enjoy!

5 thoughts on “Send your test FCM push notification with cURL”

  1. Hi!

    Thanks for the scripts! It helped us a lot !
    Just one thing, there are some typos in both scripts:

    First script : a coma is missing at the end of the first line after “TARGET_DEVICE_TOKEN”
    Both scripts : on the last line of both files, you should remove the double-quotes around https://fcm……./messages:send/

    Thanks again

  2. Nice write up. I tried this implementation and able to get the STATUS 200 from firebase but my devices are not able to get the notification.

    What do you think what could be the issue?

Leave a Reply

Your email address will not be published.