Migrate to the latest Firebase Cloud Messaging API (HTTP v1)–Firebase push notification 2024
Let’s get it straight, we need to migrate to latest Firebase Cloud Messaging API v1
- We need to setup a firebase project from firebase console either in Flutter or Native Android
- When it’s done Go to console.firebase.google.com
- Select your project
- Click on Project Settings
- Navigate to Service Accounts from Top Menu
- And click on Generate private key
Once you have downloaded the private key, follow below steps for executing the python script to get the auth token
- Install
python
- Install
pip
- Install
google-auth
- 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
Then go to your postman
- Create a new HTTP POST request
- Endpoint: https://fcm.googleapis.com/v1/projects/<FIREBASE_PROJECT_ID>/messages:send
- In my case it’s https://fcm.googleapis.com/v1/projects/push-notification-b969d/messages:send
- Add authorization in headers
- 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
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!