I am trying to set up and test push notifications using Firebase Cloud Messaging (FCM) with the new HTTP v1 API via Postman. I need to generate an OAuth 2.0 token to authenticate my requests and send a push notification to a specific device. I have the FCM service account JSON and the device token. Could someone guide me through the process of setting this up in Postman, including how to generate the OAuth 2.0 token and format the request?
Here are the details I have:
- Service account JSON file from Firebase
- Device token for the target device
- Postman for executing HTTP requests
What are the steps to generate the OAuth 2.0 token using Python and set up the Postman request to send a push notification using FCM’s HTTP v1 API?
2
Answers
I believe this might be helpful to other developers. I will try to break down the steps as easily as possible.
Step 1 - Download the Firebase Private Key
Firebase Console => Project Setting => Service Accounts => Generate new private key
Step 2 - Generate OAuth 2.0 Token using FCM service account credentials. You can you thie Python to genrate it
if you get this error
make sure to run
pip install cryptography
this will install cryptography package to your machine.The print statment will have a format of
ya29.ElqKBGN2Ri_Uz...HnS_uNreA
this is the Oauth 2.0 token.Step 3 - Postman configration, plug in the token you have found insde the postman config
you can get your project id from the url of the firebase project
Notification body
In your Flutter project create a new file and add this code:
Replace ‘path/to/service-account.json’ with the actual path to your Firebase service account JSON file.
Call the getOAuthToken() function to get the OAuth 2.0 access token.
Create a new request in Postman and set the request type to "POST".
In the request URL, enter: https://fcm.googleapis.com/v1/projects/{YOUR_PROJECT_ID}/messages:send
Replace {YOUR_PROJECT_ID} with your Firebase project ID, which you can find in the Firebase console.
In the "Headers" tab, add the following key-value pairs:
Content-Type: application/json
Authorization: Bearer {YOUR_ACCESS_TOKEN}
Replace {YOUR_ACCESS_TOKEN} with the access token you generated in the previous step.
In the "Body" tab, select "raw" and "JSON" as the data type.Then enter the following JSON payload:
Replace {DEVICE_TOKEN} with the device token you have for the target device.
Click the "Send" button in Postman to execute the request and send the push notification.
You should see a response from the FCM API indicating whether the push notification was successfully delivered.
Here’s the complete Flutter code to generate the OAuth 2.0 access token and send the push notification:
Remember to replace the following placeholders:
{YOUR_PROJECT_ID}: Your Firebase project ID
{DEVICE_TOKEN}: The device token for the target device
‘path/to/service-account.json’: The actual path to your Firebase service account JSON file