subscribe to GCM/FCM and receive notifications
python implementation of https://github.com/MatthieuLemoine/push-receiver
tested on python 2.7.16, 3.4.10 and 3.7.5
I put this together in a day or so, it's still rough around the edges,
especially the listen part, which I don't really use myself and have just
implemented for fun and only briefly tested
note that for the listening part I had to pull in http-ece which depends
on a full blown native crypto library rather than just oscrypto. it is
an optional dependency so you'll have to install it explicitly by depending
on push_receiver[listen]
usage
============
.. code-block:: sh
pip install push_receiver[listen,example]
basic usage example that stores and loads credentials and persistent ids
and prints new notifications
you can also run this example with this command (change the sender id)
.. code-block:: sh
python -m "push_receiver" --sender-id=722915550290 --app-id 1:722915550290:web:8c409a0923422212c7530
.. code-block:: python
from push_receiver import PushReceiver, register
import json
def on_notification(obj, notification, data_message):
idstr = data_message.persistent_id + "\n"
# check if we already received the notification
with open("persistent_ids.txt", "r") as f:
if idstr in f:
return
# new notification, store id so we don't read it again
with open("persistent_ids.txt", "a") as f:
f.write(idstr)
# print notification
n = notification["notification"]
text = n["title"]
if n["body"]:
text += ": " + n["body"]
print(text)
if __name__ == "__main__":
SENDER_ID = 722915550290 # change this to your sender id
APP_ID = "my:app:id" # change this to your app id
try:
# already registered, load previous credentials
with open("credentials.json", "r") as f:
credentials = json.load(f)
except FileNotFoundError:
# first time, register and store credentials
credentials = register(sender_id=SENDER_ID, app_id=APP_ID)
with open("credentials.json", "w") as f:
json.dump(credentials, f)
print("send notifications to {}".format(credentials["fcm"]["token"]))
with open("persistent_ids.txt", "a+") as f:
received_persistent_ids = [x.strip() for x in f]
receiver = PushReceiver(credentials, received_persistent_ids)
receiver.listen(on_notification)
Raw data
{
"_id": null,
"home_page": "https://github.com/olijeffers0n/push_receiver",
"name": "rustPlusPushReceiver",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "fcm gcm push notification firebase google",
"author": "Franc[e]sco",
"author_email": "lolisamurai@tfwno.gf",
"download_url": "https://files.pythonhosted.org/packages/4b/ec/12f69519305a81b0221a376dcf852f46b62c8d30ac97a5ce0a0e2ce1187b/rustPlusPushReceiver-0.4.1.tar.gz",
"platform": null,
"description": "subscribe to GCM/FCM and receive notifications\r\n\r\npython implementation of https://github.com/MatthieuLemoine/push-receiver\r\n\r\ntested on python 2.7.16, 3.4.10 and 3.7.5\r\n\r\nI put this together in a day or so, it's still rough around the edges,\r\nespecially the listen part, which I don't really use myself and have just\r\nimplemented for fun and only briefly tested\r\n\r\nnote that for the listening part I had to pull in http-ece which depends\r\non a full blown native crypto library rather than just oscrypto. it is\r\nan optional dependency so you'll have to install it explicitly by depending\r\non push_receiver[listen]\r\n\r\nusage\r\n============\r\n\r\n.. code-block:: sh\r\n\r\n pip install push_receiver[listen,example]\r\n\r\n\r\nbasic usage example that stores and loads credentials and persistent ids\r\nand prints new notifications\r\n\r\nyou can also run this example with this command (change the sender id)\r\n\r\n.. code-block:: sh\r\n\r\n python -m \"push_receiver\" --sender-id=722915550290 --app-id 1:722915550290:web:8c409a0923422212c7530\r\n\r\n\r\n.. code-block:: python\r\n\r\n from push_receiver import PushReceiver, register\r\n import json\r\n\r\n\r\n def on_notification(obj, notification, data_message):\r\n idstr = data_message.persistent_id + \"\\n\"\r\n\r\n # check if we already received the notification\r\n with open(\"persistent_ids.txt\", \"r\") as f:\r\n if idstr in f:\r\n return\r\n\r\n # new notification, store id so we don't read it again\r\n with open(\"persistent_ids.txt\", \"a\") as f:\r\n f.write(idstr)\r\n\r\n # print notification\r\n n = notification[\"notification\"]\r\n text = n[\"title\"]\r\n if n[\"body\"]:\r\n text += \": \" + n[\"body\"]\r\n print(text)\r\n\r\n\r\n if __name__ == \"__main__\":\r\n SENDER_ID = 722915550290 # change this to your sender id\r\n APP_ID = \"my:app:id\" # change this to your app id\r\n\r\n try:\r\n # already registered, load previous credentials\r\n with open(\"credentials.json\", \"r\") as f:\r\n credentials = json.load(f)\r\n\r\n except FileNotFoundError:\r\n # first time, register and store credentials\r\n credentials = register(sender_id=SENDER_ID, app_id=APP_ID)\r\n with open(\"credentials.json\", \"w\") as f:\r\n json.dump(credentials, f)\r\n\r\n print(\"send notifications to {}\".format(credentials[\"fcm\"][\"token\"]))\r\n\r\n with open(\"persistent_ids.txt\", \"a+\") as f:\r\n received_persistent_ids = [x.strip() for x in f]\r\n\r\n receiver = PushReceiver(credentials, received_persistent_ids)\r\n receiver.listen(on_notification)\r\n\r\n",
"bugtrack_url": null,
"license": "Unlicense",
"summary": "subscribe to GCM/FCM and receive notifications",
"version": "0.4.1",
"split_keywords": [
"fcm",
"gcm",
"push",
"notification",
"firebase",
"google"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4bec12f69519305a81b0221a376dcf852f46b62c8d30ac97a5ce0a0e2ce1187b",
"md5": "4db7c861cb0e0fe5bd683526c2a498e0",
"sha256": "eac2322e48cb359bdc2e98761ec3546fbcccbb43028ef5a7fb09472360494ef9"
},
"downloads": -1,
"filename": "rustPlusPushReceiver-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "4db7c861cb0e0fe5bd683526c2a498e0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12418,
"upload_time": "2022-06-28T18:11:37",
"upload_time_iso_8601": "2022-06-28T18:11:37.469149Z",
"url": "https://files.pythonhosted.org/packages/4b/ec/12f69519305a81b0221a376dcf852f46b62c8d30ac97a5ce0a0e2ce1187b/rustPlusPushReceiver-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-06-28 18:11:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "olijeffers0n",
"github_project": "push_receiver",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "rustpluspushreceiver"
}