Send Push Notifications to Google Firebase Cloud Messaging API (V1) Using Python

Tiempo de lectura: 2 minutos

Today we are going to learn how we can send a message to the Firebase Cloud Messaging endpoint using Python. (https://firebase.google.com/docs/cloud-messaging/http-server-ref?hl=en-419)

For this, we need the Google endpoint:

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send

And previously we have to generate a token in our application, for this we go to Firebase, select our project, and click on Project Settings:

Returns only the HTML translated, without any addition.

Now go to the Firebase Cloud Messaging (V1) API section

Click on Manage Service Accounts.

Go to your account and click on the 3-dot context menu:

and choose Manage Keys:

Now click on Add Key > Create new key:

Choose type JSON and click on create:

Here is the translated HTML:

Save this JSON in the root of the project and create a file named send_push.py

Now we are going to create the code send_push.py

You will need to change JSON_DIR = “app.json” to the path of your JSON.

Additionally, you need to install jwt:

 pip install PyJWT

To send a message, you can use the following:

send_cloud_message('Message Title', 'Message Body', 'device_token')

For ‘device_token’, you’ll need to use a valid device token. Here’s how you can obtain it: https://devcodelight.com/?p=7128

Leave a Comment