Skip to main content

PyFCM

version license

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

# 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

Send a data message

# With FCM, you can send two types of messages to clients:
# 1. Notification messages, sometimes thought of as "display messages."
# 2. Data messages, which are handled by the client app.
# 3. Notification messages with optional data payload.

# Client app is responsible for processing data messages. Data messages have only custom key-value pairs. (Python dict)
# Data messages let developers send up to 4KB of custom key-value pairs.

# Sending a notification with data message payload
data_payload = {
    "foo": "bar",
    "body": "great match!",
    "room": "PortugalVSDenmark"
}
# To a single device
result = fcm.notify(fcm_token=fcm_token, notification_body=notification_body, data_payload=data_payload)

# Sending a data message only payload, do NOT include notification_body also do NOT include notification body
# To a single device
result = fcm.notify(fcm_token=fcm_token, data_payload=data_payload)

# Only string key and values are accepted. booleans, nested dicts are not supported
# To send nested dict, use something like
data_payload = {
    "foo": "bar",
    "data": json.dumps(data).
}
# For more info on format see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Message
# and https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json

# Use notification messages when you want FCM to handle displaying a notification on your app's behalf.
# Use data messages when you just want to process the messages only in your app.
# PyFCM can send a message including both notification and data payloads.
# In such cases, FCM handles displaying the notification payload, and the client app handles the data payload.

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

# Send a message to devices subscribed to a topic.
result = fcm.notify(topic_name="news", notification_body=message)

# Conditional topic messaging
topic_condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
result = fcm.notify(notification_body=message, topic_condition=topic_condition)
# FCM first evaluates any conditions in parentheses, and then evaluates the expression from left to right.
# In the above expression, a user subscribed to any single topic does not receive the message. Likewise,
# a user who does not subscribe to TopicA does not receive the message. These combinations do receive it:
# TopicA and TopicB
# TopicA and TopicC
# Conditions for topics support two operators per expression, and parentheses are supported.
# For more information, check: https://firebase.google.com/docs/cloud-messaging/topic-messaging

Extra argument options

Metadata

Release files for pyfcm 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfcm 2.1.1
File Size Uploaded
pyfcm-2.1.1.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyfcm 2.1.1
File Interpreter ABI Platform
pyfcm-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.3 kB

Release files / pyfcm-2.1.1.tar.gz

Download URL pyfcm-2.1.1.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d98ad804785a2be3999746898f7582fb1b52e7b541ccb61836ad3f8f180fea5e
BLAKE2b-256 checksum
How to use checksums
3e2f1a2eb0cf6b4e72f5f2f60e824bb59b647a8caefc857dcf2965d1431faa66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pyfcm-2.1.1-py3-none-any.whl

Download URL pyfcm-2.1.1-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f07a2bfa90905206826231334481bf5d9330c66db5d5f8d32baf6ea0aaf93096
BLAKE2b-256 checksum
How to use checksums
8f3d00eb14bdacdee01bc51446c6c45037b7799e35874cba30e0a0b736bcedef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.9

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.4

2 release files

1.5.3

1 release file

1.5.2

1 release file

1.5.1

2 release files

1.5.0

2 release files

1.4.9

2 release files

1.4.7

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

0.0.9

0.0.8

0.0.7

0.0.6

0.0.5

0.0.4

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page