Git Product home page Git Product logo

push_receiver's Introduction

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
from push_receiver import register, listen
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

  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)
    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]

  listen(credentials, on_notification, received_persistent_ids)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.