Skip to main content

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

Project description


Django-Webpush
===================
[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/safwanrahman)

Django-Webpush is a Package made for integrating and sending [Web Push Notification](https://developer.mozilla.org/en/docs/Web/API/Push_API) 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

```python
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:
```python
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`](https://github.com/web-push-libs/vapid/tree/master/python) and [Google Developer Documentation](https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user#how_to_create_application_server_keys). You can obtain one easily from [web-push-codelab.glitch.me](https://web-push-codelab.glitch.me/). ``Application Server Keys`` and ``Vapid Keys`` both are same.**

Then include `webpush` in the `urls.py`

```python
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:

```python
{
"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:

```html
<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

```html
<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

```python
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:


```python
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
```python
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](http://i.imgur.com/VA6cxRc.png)


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.5.tar.gz (25.6 kB view details)

Uploaded Source

Built Distributions

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

django_webpush-0.2.5-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

django_webpush-0.2.5-py2-none-any.whl (15.6 kB view details)

Uploaded Python 2

File details

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

File metadata

File hashes

Hashes for django-webpush-0.2.5.tar.gz
Algorithm Hash digest
SHA256 60724992e38e472b774a41ad28eb02875b8e538ce9aae920c85c72c0213ea404
MD5 10503e4fc6bf3ead2840080f3bef9538
BLAKE2b-256 ad99e478c9e50f1e56a28033bcd0c7191334c178560693d400a9b8d89a469d31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_webpush-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1fbe51f861408fdda391189c76b2485de274a8f61c1edd609b59ab0b254fd25f
MD5 bbcb44e83a1ee1770e030f4002b11051
BLAKE2b-256 f2a84421993bdc0911897abba21508cba506a7575728c1f4a1e2c138e882787b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_webpush-0.2.5-py2-none-any.whl
Algorithm Hash digest
SHA256 220febfc1be45da276f09455ede9e41cbe3ba065cc28b3df41219bf2fce2be31
MD5 e1126810eb95071bc3b8d12988f35592
BLAKE2b-256 b3782d03706dde0abf7874101c9444c06076550f7b2cf450970c35f7cab8173b

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