Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #321

Merged
merged 18 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ name: Python application

on:
push:
branches: [ master ]
branches:
- '**'
create:
branches:
- '**'
tags:
- '**'
pull_request:
branches: [ master ]
branches:
- master # Run on pull requests targeting the master branch

jobs:
build:
Expand All @@ -31,6 +38,16 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Check formatting
run: |
# stop the build if there are formatting is error in any python codes
# to check whether the codes are formatted or not before merge
pip install black==24.4.2
pip install click==8.1.7
python -m black -t py310 --check .
- name: Test with pytest
run: |
pytest
export GOOGLE_APPLICATION_CREDENTIALS=service-account.json
export FCM_TEST_PROJECT_ID=test
pip install . ".[test]"
python -m pytest .
4 changes: 2 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Tests

Before commiting your changes, please run the tests. For running the tests you need an FCM API key.
olucurious marked this conversation as resolved.
Show resolved Hide resolved

**Please do not use an API key, which is used in production!**
**Please do not use a service account, which is used in production!**

::

pip install . ".[test]"

export FCM_TEST_API_KEY=AAA...
export GOOGLE_APPLICATION_CREDENTIALS="service_account.json"

python -m pytest

Expand Down
167 changes: 36 additions & 131 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*****
PyFCM
*****
|version| |license|
|version| |license|

Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)

Expand All @@ -18,14 +18,11 @@ Links
- Project: https://github.com/olucurious/pyfcm
- PyPi: https://pypi.python.org/pypi/pyfcm/

Looking for a Django version?
-----------------------------
Checkout fcm-django
- Link: https://github.com/xtrinch/fcm-django

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


Expand Down Expand Up @@ -64,32 +61,26 @@ Send notifications using the ``FCMNotification`` class
# Send to single device.
from pyfcm import FCMNotification

push_service = FCMNotification(api_key="<api-key>")
push_service = FCMNotification(service_account_file="<service-account-json-path>")

# OR initialize with proxies

proxy_dict = {
"http" : "http://127.0.0.1",
"https" : "http://127.0.0.1",
}
push_service = FCMNotification(api_key="<api-key>", proxy_dict=proxy_dict)

# Your api-key can be gotten from: https://console.firebase.google.com/project/<project-name>/settings/cloudmessaging

registration_id = "<device registration_id>"
message_title = "Uber update"
message_body = "Hi john, your customized news for today is ready"
result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)
push_service = FCMNotification(service_account_file="<service-account-json-path>", proxy_dict=proxy_dict)

# Send to multiple devices by passing a list of ids.
registration_ids = ["<device registration_id 1>", "<device registration_id 2>", ...]
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)
# Your service account file can be gotten from: https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk

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 = push_service.notify(fcm_token=fcm_token, notification_title=notification_title, notification_body=notification_body, notification_image=notification_image)
print result

|
|

Send a data message
--------------------
Expand All @@ -105,38 +96,17 @@ Send a data message
# Data messages let developers send up to 4KB of custom key-value pairs.

# Sending a notification with data message payload
data_message = {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
data_payload = {
"foo": "bar",
"body": "great match!",
"room": "PortugalVSDenmark"
}
# To multiple devices
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_body=message_body, data_message=data_message)
# To a single device
result = push_service.notify_single_device(registration_id=registration_id, message_body=message_body, data_message=data_message)
result = push_service.notify(fcm_token=fcm_token, notification_body=notification_body, data_payload=data_payload)

# Sending a data message only payload, do NOT include message_body also do NOT include notification body
# To multiple devices
result = push_service.multiple_devices_data_message(registration_ids=registration_ids, data_message=data_message)
# Sending a data message only payload, do NOT include notification_body also do NOT include notification body
# To a single device
result = push_service.single_device_data_message(registration_id=registration_id, data_message=data_message)

# To send extra kwargs (notification keyword arguments not provided in any of the methods),
# pass it as a key value in a dictionary to the method being used
extra_notification_kwargs = {
'android_channel_id': 2
}
result = push_service.notify_single_device(registration_id=registration_id, data_message=data_message, extra_notification_kwargs=extra_notification_kwargs)

# To process background notifications in iOS 10, set content_available
result = push_service.notify_single_device(registration_id=registration_id, data_message=data_message, content_available=True)

# To support rich notifications on iOS 10, set
extra_kwargs = {
'mutable_content': True
}

# and then write a NotificationService Extension in your app
result = push_service.notify(fcm_token=fcm_token, data_payload=data_payload)

# 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.
Expand All @@ -145,66 +115,28 @@ Send a data message

|

Send a low priority message
----------------------------

.. code-block:: python

# The default is low_priority == False
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_body=message, low_priority=True)

|

Get valid registration ids (useful for cleaning up invalid registration ids in your database)
---------------------------------------------------------------------------------------------

.. code-block:: python

registration_ids = ['reg id 1', 'reg id 2', 'reg id 3', 'reg id 4', ...]
valid_registration_ids = push_service.clean_registration_ids(registration_ids)
# Shoutout to @baali for this

|

Appengine users should define their environment
-----------------------------------------------

.. code-block:: python

push_service = FCMNotification(api_key="<api-key>", proxy_dict=proxy_dict, env='app_engine')
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_body=message, low_priority=True)
push_service = FCMNotification(api_key="<service-account-json-path>", proxy_dict=proxy_dict, env='app_engine')
result = push_service.notify(fcm_token=fcm_token, notification_body=message)

|

Manage subscriptions to a topic
-------------------------------

.. code-block:: python

push_service = FCMNotification(SERVER_KEY)
tokens = [
<registration_id_1>,
<registration_id_2>,
]

subscribed = push_service.subscribe_registration_ids_to_topic(tokens, 'test')
# returns True if successful, raises error if unsuccessful

unsubscribed = push_service.unsubscribe_registration_ids_from_topic(tokens, 'test')
# returns True if successful, raises error if unsuccessful

|

Sending a message to a topic
-----------------------------
.. code-block:: python

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

# Conditional topic messaging
topic_condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
result = push_service.notify_topic_subscribers(message_body=message, condition=topic_condition)
result = push_service.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:
Expand All @@ -220,51 +152,24 @@ Other argument options

::


collapse_key (str, optional): Identifier for a group of messages
that can be collapsed so that only the last message gets sent
when delivery can be resumed. Defaults to `None`.

delay_while_idle (bool, optional): If `True` indicates that the
message should not be sent until the device becomes active.

time_to_live (int, optional): How long (in seconds) the message
should be kept in FCM storage if the device is offline. The
maximum time to live supported is 4 weeks. Defaults to ``None``
which uses the FCM default of 4 weeks.

low_priority (boolean, optional): Whether to send notification with
the low priority flag. Defaults to `False`.

restricted_package_name (str, optional): Package name of the
application where the registration IDs must match in order to
receive the message. Defaults to `None`.

dry_run (bool, optional): If `True` no message will be sent but
request will be tested.

|
android_config (dict, optional): Android specific options for messages -
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconfig

Get response data
------------------
apns_config (dict, optional): Apple Push Notification Service specific options -
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig

.. code-block:: python
webpush_config (dict, optional): Webpush protocol options -
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig

# Response from PyFCM.
response_dict = {
'multicast_ids': list, # List of Unique ID (number) identifying the multicast message.
'success': int, #Number of messages that were processed without an error.
'failure': int, #Number of messages that could not be processed.
'canonical_ids': int, #Number of results that contain a canonical registration token.
'results': list, #Array of dict objects representing the status of the messages processed.
'topic_message_id': None | str
}
fcm_options (dict, optional): Platform independent options for features provided by the FCM SDKs -
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions

dry_run (bool, optional): If `True` no message will be sent but
request will be tested.

|

# registration_id: Optional string specifying the canonical registration token for the client app that the message
# was processed and sent to. Sender should use this value as the registration token for future requests. Otherwise,
# the messages might be rejected.
# error: String specifying the error that occurred when processing the message for the recipient


|

Expand Down
12 changes: 9 additions & 3 deletions pyfcm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
__version__,
__author__,
__email__,
__license__
__license__,
)
from .fcm import FCMNotification

__all__ = [
"FCMNotification", "__title__", "__summary__",
"__url__", "__version__", "__author__", "__email__", "__license__"
"FCMNotification",
"__title__",
"__summary__",
"__url__",
"__version__",
"__author__",
"__email__",
"__license__",
]
14 changes: 7 additions & 7 deletions pyfcm/__meta__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
__title__ = 'pyfcm'
__summary__ = 'Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)'
__url__ = 'https://github.com/olucurious/pyfcm'
__title__ = "pyfcm"
__summary__ = "Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)"
__url__ = "https://github.com/olucurious/pyfcm"

__version__ = '1.5.2'
__version__ = "2.0.0"

__author__ = 'Emmanuel Adegbite'
__email__ = '[email protected]'
__author__ = "Emmanuel Adegbite"
__email__ = "[email protected]"

__license__ = 'MIT License'
__license__ = "MIT License"
18 changes: 13 additions & 5 deletions pyfcm/async_fcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import aiohttp
import json

async def fetch_tasks(end_point,headers,payloads,timeout):

async def fetch_tasks(end_point, headers, payloads, timeout):
"""

:param end_point (str) : FCM endpoint
Expand All @@ -11,11 +12,18 @@ async def fetch_tasks(end_point,headers,payloads,timeout):
:param timeout (int) : FCM timeout
:return:
"""
fetches = [asyncio.Task(send_request(end_point=end_point,headers=headers,payload=payload,timeout=timeout)) for payload in payloads]
fetches = [
asyncio.Task(
send_request(
end_point=end_point, headers=headers, payload=payload, timeout=timeout
)
)
for payload in payloads
]
return await asyncio.gather(*fetches)


async def send_request(end_point,headers,payload,timeout=5):
async def send_request(end_point, headers, payload, timeout=5):
"""

:param end_point (str) : FCM endpoint
Expand All @@ -26,9 +34,9 @@ async def send_request(end_point,headers,payload,timeout=5):
"""
timeout = aiohttp.ClientTimeout(total=timeout)

async with aiohttp.ClientSession(headers=headers,timeout=timeout) as session:
async with aiohttp.ClientSession(headers=headers, timeout=timeout) as session:

async with session.post(end_point,data=payload) as res:
async with session.post(end_point, data=payload) as res:
result = await res.text()
result = json.loads(result)
return result
Loading
Loading