Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to send the headers parameters to pywebpush #118

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ WEBPUSH_SETTINGS = {
```
**Replace ``"Vapid Public Key"`` and ``"Vapid Private Key"`` with your Vapid Keys. Also replace ``[email protected]`` with your email so that the push server of browser can reach to you if anything goes wrong.**

To send push notifications to the safari browser, be sure to write the vapid email as `'[email protected]'` instead of `'mailto:[email protected]'`


> **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`
Expand Down Expand Up @@ -201,8 +204,26 @@ So in order to send notification, see below.

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




- To send push notifications to the safari browser, apple allows you to pass custom headers, you can send them as follows:

```python
from webpush import send_user_notification

headers = {"topic": "0", "urgency": "normal"}
payload = {"head": "Welcome!", "body": "Hello World"}

send_user_notification(user=user, payload=payload, ttl=1000, headers=headers)

```

You can also send custom headers, for example, apple allows you to [send extra parameters](https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_safari_and_other_browsers#3994592), then you can use the headers argument to send them.



License
=======
----
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='django-webpush',
version='0.3.5',
version='0.3.6',
packages=find_packages(),
include_package_data=True,
license='GNU Public License',
Expand All @@ -34,6 +34,6 @@
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
install_requires=[
'pywebpush==1.9.4'
'pywebpush>=1.9.4'
]
)
8 changes: 4 additions & 4 deletions webpush/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from .utils import send_notification_to_group, send_notification_to_user


def send_group_notification(group_name, payload, ttl=0):
def send_group_notification(group_name, payload, ttl=0, headers={}):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well

payload = json.dumps(payload)
send_notification_to_group(group_name, payload, ttl)
send_notification_to_group(group_name, payload, ttl, headers=headers)


def send_user_notification(user, payload, ttl=0):
def send_user_notification(user, payload, ttl=0, headers={}):
payload = json.dumps(payload)
send_notification_to_user(user, payload, ttl)
send_notification_to_user(user, payload, ttl, headers=headers)
29 changes: 21 additions & 8 deletions webpush/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
from pywebpush import WebPushException, webpush


def send_notification_to_user(user, payload, ttl=0):
def send_notification_to_user(user, payload, ttl=0, headers={}):
# Get all the push_info of the user

push_infos = user.webpush_info.select_related("subscription")
for push_info in push_infos:
_send_notification(push_info.subscription, payload, ttl)
_send_notification(push_info.subscription, payload, ttl, headers=headers)


def send_notification_to_group(group_name, payload, ttl=0):
def send_notification_to_group(group_name, payload, ttl=0, headers={}):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not good idea to take default mutable value. Can you please change it?

from .models import Group
# Get all the subscription related to the group

push_infos = Group.objects.get(name=group_name).webpush_info.select_related("subscription")
for push_info in push_infos:
_send_notification(push_info.subscription, payload, ttl)
_send_notification(push_info.subscription, payload, ttl, headers=headers)


def send_to_subscription(subscription, payload, ttl=0):
return _send_notification(subscription, payload, ttl)
def send_to_subscription(subscription, payload, ttl=0, headers={}):
return _send_notification(subscription, payload, ttl, headers=headers)


def _send_notification(subscription, payload, ttl):
def _send_notification(subscription, payload, ttl, headers={}):
subscription_data = _process_subscription_info(subscription)
vapid_data = {}

Expand All @@ -42,8 +42,21 @@ def _send_notification(subscription, payload, ttl):
'vapid_claims': {"sub": "mailto:{}".format(vapid_admin_email)}
}

endpoint = subscription_data.get("endpoint")
if endpoint.startswith("https://web.push.apple.com"):
"""
ttl, topic, urgency now are optional headers for web push notifications in Safari
Documentation:
https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_safari_and_other_browsers#3994592
"""

headers['ttl'] = str(headers.get("ttl", ttl))
headers['topic'] = str(headers.get("topic", "10"))
headers['urgency'] = headers.get("urgency", "normal")


try:
req = webpush(subscription_info=subscription_data, data=payload, ttl=ttl, **vapid_data)
req = webpush(subscription_info=subscription_data, data=payload, ttl=ttl, headers=headers, **vapid_data)
return req
except WebPushException as e:
# If the subscription is expired, delete it.
Expand Down