How to Test Firebase HTTP V1 Cloud Messaging [Push Notification] | Getting OAuth 2.0 Token

Ngima Sherpa
3 min readSep 6, 2021

If you’re using legacy Firebase Cloud Messaging and willing to migrate from legacy HTTP to HTTP v1 for Better security, more efficient customization of messages across platform & More extendable and future-proof for new client platform versions but don’t know how to test the notification like we test legacy HTTP API with POST https://fcm.googleapis.com/fcm/send. For HTTP v1 we need to use the OAuth 2.0 token instead of Server Key for Authorization. Therefore, to test the notification before backend developer implements it on his/her end. Here, I am going to show some quick steps to do so.

Step 1: Get Firebase Project Unique Identifier

Go to Firebase Console, you could see the project unique identifier under the Project Name. For example; in screenshot below ng-image-recognition is unique identifier of Image Recognition Project.

If you’re starting fresh then you could click on + Add project and you could enter Project Name and firebase will generate unique identifier based on the project name you’ve entered. Though you could edit it, but it should be unique.

Step 2: Prepare the Firebase notification push URL

In Firebase Legacy Cloud Messaging, You could use following url to send notification with Server Key in header’s Authorization like;

URL
POST https://fcm.googleapis.com/fcm/send
Header
Authorization: key=<SERVER_KEY>

But while using HTTP v1 you will be using following instead

URL
POST https://fcm.googleapis.com/v1/projects/<PROJECT_UNIQUE_ID>/messages:send
Header
Authorization: Bearer <OAUTH_2_DOT_0_ACCESS_TOKEN>

So in our case, ng-image-recognition would be the PROJECT_UNIQUE_ID

But how to get OAUTH_2_DOT_0_ACCESS_TOKEN? Let’s see it in next step:

Step 3: Generate OAUTH_2_DOT_0_ACCESS_TOKEN using Google OAuth 2.0 Playground

  • Or Copy and paste following url and paste in Input your own scopes and click on Authorize APIs https://www.googleapis.com/auth/firebase.messaging
  • And Select Account which has the ownership of the project you’re working on & Check the check box and click continue.
  • Click on Exchange authorization code for tokens button
  • Change HTTP Method to POST from default Method GET
  • Enter Request Body something like
{
"message": {
"topic": "news",
"notification": {
"title": "Breaking News",
"body": "New news story available."
},
"data": {
"story_id": "story_12345"
},
"android": {
"notification": {
"click_action": "TOP_STORY_ACTIVITY"
}
},
"apns": {
"payload": {
"aps": {
"category" : "NEW_MESSAGE_CATEGORY"
}
}
}
}
}
  • And finally Send the request
  • If you have followed the steps above correctly then you should receive response like this
{
"name": "projects/ng-image-recognition/messages/90096515223"
}

Keep in mind that the token generated will be valid for 1 hr only. But you could regenerate it from Step 2 in OAuth 2.0 Playground.

You could find lot more details about the request data formats from Firebase Official HTTP V1 Migration Guide.

--

--

Ngima Sherpa
Ngima Sherpa

Written by Ngima Sherpa

Mobile Developer | UI Designer

No responses yet