This project demonstrates how to send notifications to Android and iOS devices using the Firebase Cloud Messaging (FCM) HTTP v1 API. It generates OAuth2 access tokens using a service account and sends notifications to a device using the FCM token.
Before running the project, ensure you have the following:
- Firebase Project: You must have a Firebase project created and set up for FCM.
- Service Account JSON File: Download the service account credentials from Firebase Console.
- FCM Token: The target device must have an FCM token generated by the Firebase SDK.
-
Clone the repository:
git clone https://github.com/ksaurabh4/fcm-http-v1-notification-sender.git cd fcm-http-v1-notification-sender
-
Install the required Python packages:
pip install google-auth requests
-
Service Account Setup:
- You can provide the path to your service account JSON file or paste its content directly into the code.
Option 1: Provide the file path of your service account JSON:
SERVICE_ACCOUNT_FILE = './path_to_your_serviceAccountKey.json'
Option 2: Paste the JSON content directly into
SERVICE_ACCOUNT_INFO
in the code:SERVICE_ACCOUNT_INFO = { "type": "", "project_id": "", "private_key_id": "", "private_key": "", "client_email": "", "client_id": "", "auth_uri": "", "token_uri": "", "auth_provider_x509_cert_url": "", "client_x509_cert_url": "", "universe_domain": "" }
-
Firebase Project ID:
Set your Firebase project ID in the
PROJECT_ID
variable:PROJECT_ID = "your-project-id"
-
Target Device FCM Token:
Set the target device's FCM token in the
TARGET_FCM_TOKEN
variable:TARGET_FCM_TOKEN = "your-device-fcm-token"
-
Notification Details:
Customize the notification title and body by modifying the
NOTIFICATION_TITLE
andNOTIFICATION_BODY
variables:NOTIFICATION_TITLE = 'Your Notification Title' NOTIFICATION_BODY = 'Your notification body message.'
-
Run the Python script to send a notification:
python fcm_sender.py
-
Upon successful execution, the script will send a notification to the target device and display the response in the console.
You can also run this script directly in Google Colab by uploading your service account key and running the code. To do so:
- Open Google Colab by clicking here.
- Upload your service account JSON file to Colab.
- Update the
SERVICE_ACCOUNT_FILE
path or paste the JSON content as per the instructions above. - Run the code to send the notification.
If you'd prefer to use the pre-existing Colab notebook, you can click here to open the notebook.
Colab is a convenient cloud-based environment that doesn't require local setup, making it easy to experiment and run Python scripts from anywhere.
If you want to include a custom data payload along with the notification, you can add it to the message
dictionary:
message = {
"message": {
"token": token,
"notification": {
"title": title,
"body": body
},
"data": {
"key1": "value1",
"key2": "value2"
}
}
}
In case of issues with sending the message or generating the access token, appropriate error messages will be printed to the console, detailing the cause of failure.
This project is licensed under the MIT License. See the LICENSE file for details.