|
| 1 | +|Build\_Status| [|Requirements Status|] |
| 2 | + |
| 3 | +Webpush Data encryption library for Python |
| 4 | +========================================== |
| 5 | + |
| 6 | +This is a work in progress. This library is available on `pypi as |
| 7 | +pywebpush <https://pypi.python.org/pypi/pywebpush>`__. Source is |
| 8 | +available on `github <https://github.com/jrconlin/pywebpush>`__ |
| 9 | + |
| 10 | +Installation |
| 11 | +------------ |
| 12 | + |
| 13 | +You'll need to run ``python virtualenv``. Then |
| 14 | + |
| 15 | +:: |
| 16 | + |
| 17 | + bin/pip install -r requirements.txt |
| 18 | + bin/python setup.py develop |
| 19 | + |
| 20 | +Usage |
| 21 | +----- |
| 22 | + |
| 23 | +In the browser, the promise handler for |
| 24 | +`registration.pushManager.subscribe() <https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe>`__ |
| 25 | +returns a |
| 26 | +`PushSubscription <https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription>`__ |
| 27 | +object. This object has a .toJSON() method that will return a JSON |
| 28 | +object that contains all the info we need to encrypt and push data. |
| 29 | + |
| 30 | +As illustration, a subscription info object may look like: |
| 31 | + |
| 32 | +:: |
| 33 | + |
| 34 | + {"endpoint": "https://updates.push.services.mozilla.com/push/v1/gAA...", "keys": {"auth": "k8J...", "p256dh": "BOr..."}} |
| 35 | + |
| 36 | +How you send the PushSubscription data to your backend, store it |
| 37 | +referenced to the user who requested it, and recall it when there's new |
| 38 | +a new push subscription update is left as an excerise for the reader. |
| 39 | + |
| 40 | +The data can be any serial content (string, bit array, serialized JSON, |
| 41 | +etc), but be sure that your receiving application is able to parse and |
| 42 | +understand it. (e.g. ``data = "Mary had a little lamb."``) |
| 43 | + |
| 44 | +gcm\_key is the API key obtained from the Google Developer Console. It |
| 45 | +is only needed if endpoint is https://android.googleapis.com/gcm/send |
| 46 | + |
| 47 | +``headers`` is a ``dict``\ ionary of additional HTTP header values (e.g. |
| 48 | +`VAPID <https://github.com/mozilla-services/vapid/tree/master/python>`__ |
| 49 | +self identification headers). It is optional and may be omitted. |
| 50 | + |
| 51 | +to send: |
| 52 | + |
| 53 | +:: |
| 54 | + |
| 55 | + WebPusher(subscription_info).send(data, headers) |
| 56 | + |
| 57 | +to send for Chrome: |
| 58 | + |
| 59 | +:: |
| 60 | + |
| 61 | + WebPusher(subscription_info).send(data, headers, ttl, gcm_key) |
| 62 | + |
| 63 | +You can also simply encode the data to send later by calling |
| 64 | + |
| 65 | +:: |
| 66 | + |
| 67 | + encoded = WebPush(subscription_info).encode(data) |
| 68 | + |
| 69 | +.. |Build\_Status| image:: https://travis-ci.org/jrconlin/pywebpush.svg?branch=master |
| 70 | + :target: https://travis-ci.org/jrconlin/pywebpush |
| 71 | +.. |Requirements Status| image:: https://requires.io/github/web-push-libs/pywebpush/requirements.svg?branch=master |
| 72 | + |
0 commit comments