Migrate to the latest Firebase Cloud Messaging API (HTTP v1)–Firebase push notification 2024

Apoorv Pandey
3 min readSep 9, 2024

--

Let’s get it straight, we need to migrate to latest Firebase Cloud Messaging API v1

  1. We need to setup a firebase project from firebase console either in Flutter or Native Android
  2. When it’s done Go to console.firebase.google.com
  3. Select your project
  4. Click on Project Settings
  5. Navigate to Service Accounts from Top Menu
  6. And click on Generate private key
Service accounts tab for generating a private key

Once you have downloaded the private key, follow below steps for executing the python script to get the auth token

  1. Install python
  2. Install pip
  3. Install google-auth
  4. Install requests

Then run the below python code:

from google.oauth2 import service_account
import google.auth.transport.requests

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']

def _get_access_token():
credentials = service_account.Credentials.from_service_account_file(
'/Users/apoorvpandey/Desktop/push-notification-b969d-firebase-adminsdk-oh009-30975db38d.json',// PATH to Private key which we downloaded
scopes=SCOPES)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
return credentials.token

if __name__ == "__main__":
token = _get_access_token()
print(f"Access Token: {token}")

After this you should be able to see the Access Token printed in your terminal

Access Token

Then go to your postman

  1. Create a new HTTP POST request
  2. Endpoint: https://fcm.googleapis.com/v1/projects/<FIREBASE_PROJECT_ID>/messages:send
  3. In my case it’s https://fcm.googleapis.com/v1/projects/push-notification-b969d/messages:send
  4. Add authorization in headers
  5. Authorization Bearer <ACCESS_TOKEN>

Below is the screenshot for postman request

Body payload for sending notification

{
"message": {
"token": "FCM_TOKEN", // FCM token of device to which you want to send notification
"notification": {
"title": "Hi, you have a new notifiation", // Notification title
"body": "Someone has sent you a new message, check it out!" // Notification content
}
}
}

To get the FCM token follow this article in which I have explained on how to get FCM token in Flutter App

Once you have done everything you should be able to see the notification sent from POSTMAN

Received notification

You can also send data in your notification just like this in below:

{
"message": {
"token": "FCM_TOKEN", // FCM token of device to which you want to send notification
"notification": {
"title": "Hi, you have a new notifiation", // Notification title
"body": "Someone has sent you a new message, check it out!" // Notification content
},
"data": {
"screen": "/notifications"
}
}
}

Any problems/suggestion reach me out or comment.

Thanks!

GitHub Repo here for flutter project

--

--

Apoorv Pandey

👨‍💻 Passionate Software Engineer diving into the digital realm and beyond! 💻✨