PyFCM
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)
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
from pyfcm import FCMNotification
fcm = FCMNotification(service_account_file="<service-account-json-path>", project_id="<project-id>")
fcm = FCMNotification(
service_account_file=None, credentials=your_credentials, project_id="<project-id>"
)
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)
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>")
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
Send a data message
data_payload = {
"foo": "bar",
"body": "great match!",
"room": "PortugalVSDenmark"
}
result = fcm.notify(fcm_token=fcm_token, notification_body=notification_body, data_payload=data_payload)
result = fcm.notify(fcm_token=fcm_token, data_payload=data_payload)
data_payload = {
"foo": "bar",
"data": json.dumps(data).
}
Appengine users should define their environment
fcm = FCMNotification(service_account_file="<service-account-json-path>", project_id="<project-id>", proxy_dict=proxy_dict, env='app_engine')
result = fcm.notify(fcm_token=fcm_token, notification_body=message)
Sending a message to a topic
result = fcm.notify(topic_name="news", notification_body=message)
topic_condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
result = fcm.notify(notification_body=message, topic_condition=topic_condition)