Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Django-Webpush
==============
[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/safwanrahman)
Expand All @@ -9,7 +9,6 @@ Currently, it Supports Sending Push Notification to **Firefox 46+, Chrome 52+ an

----------


Installation and Setup
----------------------

Expand All @@ -27,13 +26,15 @@ INSTALLED_APPS = (
```

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_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.**

**Generate a Vapid key pair**
Expand All @@ -45,11 +46,20 @@ python manage.py webpush_generate_vapid_keypair
Then include `webpush` in the `urls.py`

```python
# Django >= 2.0
from django.urls import path, include

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

# Django < 2.0
from django.conf.urls import url, include

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,
Expand All @@ -71,15 +81,13 @@ TEMPLATES = [
]
```


**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.

Expand All @@ -89,17 +97,21 @@ Next, inside the `<head></head>` tag add `webpush_header` according to your temp
<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>
Expand All @@ -110,8 +122,10 @@ Or if you want to add custom classes (e.g. bootstrap)
```html
<body>
<p> Hello World! </p>

# For django templating engine
{% webpush_button with_class="btn btn-outline-info" %}

# For jinja templating engine
{{ webpush_button(with_class="btn btn-outline-info") }}
</body>
Expand All @@ -126,6 +140,7 @@ Or if you want to add custom classes (e.g. bootstrap)

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.

----------
Expand All @@ -138,7 +153,6 @@ 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

Expand All @@ -152,6 +166,7 @@ So in order to send notification, see below.
```

- If you would like to send Notification to a specific user, do like following

```python
from webpush import send_user_notification

Expand All @@ -178,14 +193,14 @@ So in order to send notification, see below.

send_group_notification(group_name="my_group", payload=payload, ttl=1000)
```

**And the subscribers will get a notification like:**

![Web Push Notification icon](http://i.imgur.com/Vr1RMvF.png)

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

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

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

```python
from webpush.utils import send_to_subscription
Expand All @@ -198,11 +213,7 @@ So in order to send notification, see below.
send_to_subscription(push_info.subscription, payload)

```





**And the subscribers will get a notification like**
![Web Push Notification](http://i.imgur.com/VA6cxRc.png)

Expand All @@ -218,20 +229,20 @@ The package is shipped with built in internationalization support.

If you would like to add more language or update translation, you can run the following command:

```bash
```bash

# Add js translation
django-admin makemessages -d djangojs -l <language_code>
# Add js translation
django-admin makemessages -d djangojs -l <language_code>

# Add python translation
django-admin makemessages -l <language_code>
```
# Add python translation
django-admin makemessages -l <language_code>
```

After that, you can run `django-admin compilemessages` to compile the messages.

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.
Expand Down
2 changes: 2 additions & 0 deletions webpush/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from .utils import send_notification_to_group, send_notification_to_user


def send_group_notification(group_name, payload, ttl=0, exclude_user_id=None):
payload = json.dumps(payload)
send_notification_to_group(group_name, payload, ttl, exclude_user_id)


def send_user_notification(user, payload, ttl=0):
payload = json.dumps(payload)
send_notification_to_user(user, payload, ttl)
8 changes: 4 additions & 4 deletions webpush/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def save_or_delete(self, subscription, user, status_type, group_name):
data["user"] = user

if group_name:
group, created = Group.objects.get_or_create(name=group_name)
group, _ = Group.objects.get_or_create(name=group_name)
data["group"] = group

data["subscription"] = subscription

push_info, created = PushInformation.objects.get_or_create(**data)
push_info, _ = PushInformation.objects.get_or_create(**data)

# If unsubscribe is called, that means need to delete the browser
# and notification info from server.
# and notification info from server.
if status_type == "unsubscribe":
push_info.delete()
subscription.delete()
Expand All @@ -39,5 +39,5 @@ class Meta:
fields = ('endpoint', 'auth', 'p256dh', 'browser', 'user_agent')

def get_or_save(self):
subscription, created = SubscriptionInfo.objects.get_or_create(**self.cleaned_data)
subscription, _ = SubscriptionInfo.objects.get_or_create(**self.cleaned_data)
return subscription
2 changes: 1 addition & 1 deletion webpush/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST, require_GET
from django.views.decorators.http import require_POST
from django.views.generic import TemplateView

from .forms import WebPushForm, SubscriptionForm
Expand Down