|
| 1 | +# PyFCM |
| 2 | + |
| 3 | +[](https://pypi.python.org/pypi/pyfcm/) |
| 4 | +[](https://pypi.python.org/pypi/pyfcm/) |
| 5 | + |
| 6 | +Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web) |
| 7 | + |
| 8 | +Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits |
| 9 | +the reliable and scalable GCM infrastructure, plus new features. GCM |
| 10 | +users are strongly recommended to upgrade to FCM. |
| 11 | + |
| 12 | +Using FCM, you can notify a client app that new email or other data is |
| 13 | +available to sync. You can send notifications to drive user reengagement |
| 14 | +and retention. For use cases such as instant messaging, a message can |
| 15 | +transfer a payload of up to 4KB to a client app. |
| 16 | + |
| 17 | +For more information, visit: |
| 18 | +<https://firebase.google.com/docs/cloud-messaging/> |
| 19 | + |
| 20 | +## Links |
| 21 | + |
| 22 | +- Project: <https://github.com/olucurious/pyfcm> |
| 23 | +- PyPi: <https://pypi.python.org/pypi/pyfcm/> |
| 24 | + |
| 25 | +### Updates (Breaking Changes) |
| 26 | + |
| 27 | +- MIGRATION TO FCM HTTP V1 (JUNE 2024): |
| 28 | + <https://github.com/olucurious/PyFCM/releases/tag/2.0.0> (big |
| 29 | + shoutout to @Subhrans for the PR, for more information: |
| 30 | + <https://firebase.google.com/docs/cloud-messaging/migrate-v1>) |
| 31 | +- MAJOR UPDATES (AUGUST 2017): |
| 32 | + <https://github.com/olucurious/PyFCM/releases/tag/1.4.0> |
| 33 | + |
| 34 | +Installation ========== |
| 35 | + |
| 36 | +Install using pip: |
| 37 | + |
| 38 | + pip install pyfcm |
| 39 | + |
| 40 | + OR |
| 41 | + |
| 42 | + pip install git+https://github.com/olucurious/PyFCM.git |
| 43 | + |
| 44 | +PyFCM supports Android, iOS and Web. |
| 45 | + |
| 46 | +## Features |
| 47 | + |
| 48 | +- All FCM functionality covered |
| 49 | +- Tornado support |
| 50 | + |
| 51 | +## Examples |
| 52 | + |
| 53 | +### Send notifications using the `FCMNotification` class |
| 54 | + |
| 55 | +``` python |
| 56 | +# Send to single device. |
| 57 | +from pyfcm import FCMNotification |
| 58 | + |
| 59 | +push_service = FCMNotification(service_account_file="<service-account-json-path>") |
| 60 | + |
| 61 | +# OR initialize with proxies |
| 62 | + |
| 63 | +proxy_dict = { |
| 64 | + "http" : "http://127.0.0.1", |
| 65 | + "https" : "http://127.0.0.1", |
| 66 | + } |
| 67 | +push_service = FCMNotification(service_account_file="<service-account-json-path>", proxy_dict=proxy_dict) |
| 68 | + |
| 69 | +# Your service account file can be gotten from: https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk |
| 70 | + |
| 71 | +fcm_token = "<fcm token>" |
| 72 | +notification_title = "Uber update" |
| 73 | +notification_body = "Hi John, your order is on the way!" |
| 74 | +notification_image = "https://example.com/image.png" |
| 75 | +result = push_service.notify(fcm_token=fcm_token, notification_title=notification_title, notification_body=notification_body, notification_image=notification_image) |
| 76 | +print result |
| 77 | +``` |
| 78 | + |
| 79 | +### Send a data message |
| 80 | + |
| 81 | +``` python |
| 82 | +# With FCM, you can send two types of messages to clients: |
| 83 | +# 1. Notification messages, sometimes thought of as "display messages." |
| 84 | +# 2. Data messages, which are handled by the client app. |
| 85 | +# 3. Notification messages with optional data payload. |
| 86 | + |
| 87 | +# Client app is responsible for processing data messages. Data messages have only custom key-value pairs. (Python dict) |
| 88 | +# Data messages let developers send up to 4KB of custom key-value pairs. |
| 89 | + |
| 90 | +# Sending a notification with data message payload |
| 91 | +data_payload = { |
| 92 | + "foo": "bar", |
| 93 | + "body": "great match!", |
| 94 | + "room": "PortugalVSDenmark" |
| 95 | +} |
| 96 | +# To a single device |
| 97 | +result = push_service.notify(fcm_token=fcm_token, notification_body=notification_body, data_payload=data_payload) |
| 98 | + |
| 99 | +# Sending a data message only payload, do NOT include notification_body also do NOT include notification body |
| 100 | +# To a single device |
| 101 | +result = push_service.notify(fcm_token=fcm_token, data_payload=data_payload) |
| 102 | + |
| 103 | +# Use notification messages when you want FCM to handle displaying a notification on your app's behalf. |
| 104 | +# Use data messages when you just want to process the messages only in your app. |
| 105 | +# PyFCM can send a message including both notification and data payloads. |
| 106 | +# In such cases, FCM handles displaying the notification payload, and the client app handles the data payload. |
| 107 | +``` |
| 108 | + |
| 109 | +### Appengine users should define their environment |
| 110 | + |
| 111 | +``` python |
| 112 | +push_service = FCMNotification(api_key="<service-account-json-path>", proxy_dict=proxy_dict, env='app_engine') |
| 113 | +result = push_service.notify(fcm_token=fcm_token, notification_body=message) |
| 114 | +``` |
| 115 | + |
| 116 | +### Sending a message to a topic |
| 117 | + |
| 118 | +``` python |
| 119 | +# Send a message to devices subscribed to a topic. |
| 120 | +result = push_service.notify(topic_name="news", notification_body=message) |
| 121 | + |
| 122 | +# Conditional topic messaging |
| 123 | +topic_condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)" |
| 124 | +result = push_service.notify(notification_body=message, topic_condition=topic_condition) |
| 125 | +# FCM first evaluates any conditions in parentheses, and then evaluates the expression from left to right. |
| 126 | +# In the above expression, a user subscribed to any single topic does not receive the message. Likewise, |
| 127 | +# a user who does not subscribe to TopicA does not receive the message. These combinations do receive it: |
| 128 | +# TopicA and TopicB |
| 129 | +# TopicA and TopicC |
| 130 | +# Conditions for topics support two operators per expression, and parentheses are supported. |
| 131 | +# For more information, check: https://firebase.google.com/docs/cloud-messaging/topic-messaging |
| 132 | +``` |
| 133 | + |
| 134 | +### Other argument options |
| 135 | + |
| 136 | +: |
| 137 | + |
| 138 | + android_config (dict, optional): Android specific options for messages - |
| 139 | + https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconfig |
| 140 | + |
| 141 | + apns_config (dict, optional): Apple Push Notification Service specific options - |
| 142 | + https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig |
| 143 | + |
| 144 | + webpush_config (dict, optional): Webpush protocol options - |
| 145 | + https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig |
| 146 | + |
| 147 | + fcm_options (dict, optional): Platform independent options for features provided by the FCM SDKs - |
| 148 | + https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions |
| 149 | + |
| 150 | + dry_run (bool, optional): If `True` no message will be sent but |
| 151 | + request will be tested. |
0 commit comments