Skip to main content

subscribe to GCM/FCM and receive notifications

Project description

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

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)

python -m "push_receiver" --sender-id=722915550290 --app-id 1:722915550290:web:8c409a0923422212c7530
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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rustPlusPushReceiver-0.4.0.tar.gz (12.4 kB view details)

Uploaded Source

File details

Details for the file rustPlusPushReceiver-0.4.0.tar.gz.

File metadata

  • Download URL: rustPlusPushReceiver-0.4.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.0

File hashes

Hashes for rustPlusPushReceiver-0.4.0.tar.gz
Algorithm Hash digest
SHA256 cee677f6ae74f4c1996daf444a9a0ce98a677df22506ccf53c3b4d5cc6443ac7
MD5 14351782ff729977c9c014a0518d9f89
BLAKE2b-256 6d43ed243859dce99005e000d028794a67bde10fd12fd8e8457a19cd3bce09fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page