Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)
Maintained. Last release 3 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install pyfcmPyFCM
Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)
Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features. GCM users are strongly recommended to upgrade to FCM.
Using FCM, you can notify a client app that new email or other data is available to sync. You can send notifications to drive user reengagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.
For more information, visit: https://firebase.google.com/docs/cloud-messaging/
Links
Updates (Breaking Changes)
- MIGRATION TO FCM HTTP V1 (JUNE 2024): https://github.com/olucurious/PyFCM/releases/tag/2.0.0 (big shoutout to @Subhrans for the PR, for more information: https://firebase.google.com/docs/cloud-messaging/migrate-v1)
- MAJOR UPDATES (AUGUST 2017): https://github.com/olucurious/PyFCM/releases/tag/1.4.0
Installation ==========
Install using pip:
pip install pyfcm
OR
pip install git+https://github.com/olucurious/PyFCM.git
PyFCM supports Android, iOS and Web.
Features
- All FCM functionality covered
- Tornado support
Examples
Send notifications using the FCMNotification class
# Send to single device.
from pyfcm import FCMNotification
fcm = FCMNotification(service_account_file="<service-account-json-path>", project_id="<project-id>")
# Google oauth2 credentials(such as ADC, impersonate credentials) can be used instead of service account file.
fcm = FCMNotification(
service_account_file=None, credentials=your_credentials, project_id="<project-id>"
)
# OR initialize with proxies
proxy_dict = {
"http" : "http://127.0.0.1",
"https" : "http://127.0.0.1",
}
fcm = FCMNotification(service_account_file="<service-account-json-path>", project_id="<project-id>", proxy_dict=proxy_dict)
# OR using credentials from environment variable
# Often you would save service account json in evironment variable
# Assuming GCP_CREDENTIALS contains the data (TIP: use "export GCP_CREDENTIALS=$(filename.json)" to quickly load the json)
from google.oauth2 import service_account
gcp_json_credentials_dict = json.loads(os.getenv('GCP_CREDENTIALS', None))
credentials = service_account.Credentials.from_service_account_info(gcp_json_credentials_dict, scopes=['https://www.googleapis.com/auth/firebase.messaging'])
fcm = FCMNotification(service_account_file=None, credentials=credentials, project_id="<project-id>")
# Your service account file can be gotten from: https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk
# Now you are ready to send notification
fcm_token = "<fcm token>"
notification_title = "Uber update"
notification_body = "Hi John, your order is on the way!"
notification_image = "https://example.com/image.png"
result = fcm.notify(fcm_token=fcm_token, notification_title=notification_title, notification_body=notification_body, notification_image=notification_image)
print result