Skip to main content

A clean interface to pywebpush from Flask

Project description

Flask-WebPush

A clean interface to pywebpush from Flask. Basically, a very thin wrapper.

Quickstart

Instantiation

Instantiate this extension like any other Flask extension:

from flask_pywebpush import WebPush
app = Flask()
push = WebPush(app)

Or you may initialize the extension with the Flask instance later:

from flask_pywebpush import WebPush
app = Flask()
push = WebPush()
push.init_app(app)

VAPID keys and sender info

WebPush sender requires a private key and an identification URL to operate. For more information on those values, see this article on Google Developers.

You may specify these values to the extension by setting the following configuration variables of the Flask instance:

  • WEBPUSH_VAPID_PRIVATE_KEY The application server private key. As with pywebpush, it may either be the literal key value (DER representation) or a path to a VAPID EC2 private key PEM file;
  • WEBPUSH_SENDER_INFO: a URL containing information to contact the notification sender. Commonly a mailto: URL.

Alternatively, you can pass these values on creation of the WebPush extension instance or to the init_app method later on:

from flask_pywebpush import WebPush
push = WebPush(private_key='some_value',
               sender_info='mailto:admin@server.com')

Notification sending

Assuming you've got the subscription storage done, you may send a notification to a given subscription object like so:

from flask_pywebpush import WebPushException

subscription = {
    'endpoint': '---some-value---',
    'keys': { ... }
}
notification = {
    'title': 'Test',
    'body': 'notification body',
}

try:
    push.send(subscription, notification)
except WebPushException as exc:
    print(exc)

You can also pass keyword arguments like verbose=True to .send(), which will be directly passed to pywebpush.webpush()

Extended Usage

Since this is a wrapper over pywebpush, it exposes the main objects of the underlying library, namely, webpush, WebPushException, WebPusher.
All of them may be directly imported from flask_pywebpush.

The WebPush extension instance also has a get_webpusher(subscription) method, allowing you to get a pywebpush.WebPusher instance for a given subscription.

If your usage requires more VAPID claims than simply {"sub": "sender_info"}, then you may override the passed claims using the set_claims method:

from flask_pywebpush import WebPush
push = WebPush()
push.set_claims({})  # whatever VAPID claims you might need

License

MIT

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

Flask-pyWebPush-1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_pyWebPush-1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file Flask-pyWebPush-1.0.tar.gz.

File metadata

  • Download URL: Flask-pyWebPush-1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for Flask-pyWebPush-1.0.tar.gz
Algorithm Hash digest
SHA256 6d11a38febe4a03167fb63922446212d0b40d9d7898046bfee59af8a3afb90f0
MD5 c2c871e9a008f562923396e790291093
BLAKE2b-256 48bb3427cae526b30a8b5ca8995b62f49dc30fed69280fd7fd4ff8879bed437d

See more details on using hashes here.

File details

Details for the file Flask_pyWebPush-1.0-py3-none-any.whl.

File metadata

  • Download URL: Flask_pyWebPush-1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for Flask_pyWebPush-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f228fa7032277d778a3a9514c9365820cb6b1435bd521b7cff79534cc27fd54
MD5 8491ac728eae57a3bfb6465d664c4f3a
BLAKE2b-256 b79e17fa0d42d460545e66d5f3d2515ff58bf1bfcafd3e6b2a28dc177cc4c7ca

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