Skip to main content

A simple Django package to integrate Web Push Notification in your Application

Project description

Django-Webpush

Say Thanks!

Django-Webpush is a Package made for integrating and sending Web Push Notification in Django Application.

Currently, it Supports Sending Push Notification to Firefox 46+ and Chrome 52+.


Installation and Setup

You can install it easily from pypi by running

pip install django-webpush

After installing the package, add webpush in in your INSTALLED_APPS settings

INSTALLED_APPS = (
    ...
    'webpush',
)

If you would like to send notification to Google Chrome Users, you need to add a WEBPUSH_SETTINGS entry with the Vapid Credentials Like following:

WEBPUSH_SETTINGS = {
    "VAPID_PUBLIC_KEY": "Vapid Public Key",
    "VAPID_PRIVATE_KEY":"Vapid Private Key",
    "VAPID_ADMIN_EMAIL": "admin@example.com"
}

Replace "Vapid Public Key" and "Vapid Private Key" with your Vapid Keys. Also replace admin@example.com with your email so that the push server of browser can reach to you if anything goes wrong.

To know how to obtain Vapid Keys please see this py_vapid and Google Developer Documentation. You can obtain one easily from web-push-codelab.glitch.me. Application Server Keys and Vapid Keys both are same.

Then include webpush in the urls.py

urlpatterns =  [
    url(r'^webpush/', include('webpush.urls'))
]

django-webpush is shipped with built in jinja support. If you would like to use with jinja backend, pass pipeline.jinja2.PipelineExtension to your jinja environment. Like following:

{
    "BACKEND": "django_jinja.backend.Jinja2",
    "OPTIONS": {
      'extensions': ['webpush.jinja2.WebPushExtension'],
    }
},

Then run Migration by python manage.py migrate

Adding Web Push Information in Template

So in template, you need to load webpush_notifications custom template tag by following:

  • If you are using built in templating engine, add {% load webpush_notifications %} in the template
  • If you are using jinja templating engine, you do not need to load anything.

Next, inside the <head></head> tag add webpush_header according to your templating engine:

<head>
  # For django templating engine
  {% webpush_header %}
  # For jinja templating engine
  {{ webpush_header() }}
</head>

Next, inside the <body></body> tag, insert webush_button where you would like to see the Subscribe to Push Messaging Button. Like following

<body>
  <p> Hello World! </p>
  # For django templating engine
  {% webpush_button %}
  # For jinja templating engine
  {{ webpush_button() }}
</body>

Note: The Push Notification Button will show only if the user is logged in or any group named is passed through webpush context

If you would like to mark the subscription as a group, like all person subscribe for push notification from the template should be marked as group and would get same notification, you should pass a webpush context to the template through views. The webpush context should have a dictionary like {"group": group_name} . Like following

 webpush = {"group": group_name } # The group_name should be the name you would define.

return render(request, 'template.html',  {"webpush":webpush})

Note: If you dont pass group through the webpush context, only logged in users can see the button for subscription and able to get notification.


Sending Web Push Notification

A Web Push generally have a header and body. According to the W3C Specification, the data should be encrypted in transmission. The data is addressed as payload generally. Also a TTL header should be included indicating how much time the web push server store the data if the user is not online. So in order to send notification, see below.

  • If you would like to send notification to a specific group, do like following:

    from webpush import send_group_notification
    
    payload = {"head": "Welcome!", "body": "Hello World"}
    
    send_group_notification(group_name="my_group", payload=payload, ttl=1000)
    # All subscribe subscribe through "my_group" will get a web push notification.
    # A ttl of 1000 is passed so the web push server will store
    # the data maximum 1000 seconds if any user is not online
    
  • If you would like to send Notification to a specific user, do like following

    from webpush import send_user_notification
    
    payload = {"head": "Welcome!", "body": "Hello World"}
    
    send_user_notification(user=user, payload=payload, ttl=1000)
    # Here in the user parameter, a user object should be passed
    # The user will get notification to all of his subscribed browser. A user can subscribe many browsers.
    

    And the subscribers will get a notification like:

Web Push Notification

  • If you notification should have an icon or open a url when clicked, you can add those to the payload:

    from webpush import send_user_notification
    
    from webpush import send_group_notification
    
    payload = {"head": "Welcome!", "body”: "Hello World", 
               "icon": "https://i.imgur.com/dRDxiCQ.png“, "url": "https://www.example.com"}
    
    send_group_notification(group_name="my_group", payload=payload, ttl=1000)
    

And the subscribers will get a notification like:

Web Push Notification icon

That will open https://www.example.com if clicked.

  • If you want fine grained control over sending a single push message, do like following

    from webpush import WebPushException, send_single_notification
    
    payload = {"head": "Welcome!", "body": "Hello World"}
    
    user = request.user
    push_infos = user.webpush_info.select_related("subscription")
    for push_info in push_infos:
        try:
            send_single_notification(push_info.subscription, payload)
    
        except WebPushException as ex:
            extra = {"subscription": push_info.subscription, "exception": ex}
            log.error("Sending Push notification failed.", extra=extra)
            if "<Response [410]>" in str(ex):
                # 410 = Endpoint Not Valid
                # remove subscription
                push_info.subscription.delete()
    

And the subscribers will get a notification like Web Push Notification

License


Copyright © 2018 Safwan Rahman

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

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

django-webpush-0.2.7.tar.gz (26.8 kB view details)

Uploaded Source

Built Distributions

django_webpush-0.2.7-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

django_webpush-0.2.7-py2-none-any.whl (16.4 kB view details)

Uploaded Python 2

File details

Details for the file django-webpush-0.2.7.tar.gz.

File metadata

  • Download URL: django-webpush-0.2.7.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.2 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for django-webpush-0.2.7.tar.gz
Algorithm Hash digest
SHA256 2e561287ef61aaea353864686ab6993929cf347e1f511229831ae61de704bd79
MD5 425dff15329fd3443c7ac2516938a4c4
BLAKE2b-256 7ed57be9d9b26ef803d81f14083ca0805f6b48151c356daf343179cfe85cc66c

See more details on using hashes here.

File details

Details for the file django_webpush-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: django_webpush-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for django_webpush-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 727ce18ad8e133056b8d24dbe4203332bca3c5b79b496d3c86f077d229f21faa
MD5 75ad643fa774f885b618833db55f60e8
BLAKE2b-256 32f3020e4e4c8727d39de6b0005c9b16bc725bf8bfb416544405aa75a55b8c89

See more details on using hashes here.

File details

Details for the file django_webpush-0.2.7-py2-none-any.whl.

File metadata

  • Download URL: django_webpush-0.2.7-py2-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.4.2 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/2.7.14

File hashes

Hashes for django_webpush-0.2.7-py2-none-any.whl
Algorithm Hash digest
SHA256 95563242747a1eae37525ca107648a07b4c26fba65185bca5c4dd0448028f8b0
MD5 f3e064d7387086bae8daacf644452231
BLAKE2b-256 b2f87a6d7ca216d031a60f9afcc1e7a354e913db24194d8ea853f03d542bd1a8

See more details on using hashes here.

Supported by

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