-
Notifications
You must be signed in to change notification settings - Fork 109
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
aleaxis09
wants to merge
18
commits into
safwanrahman:master
Choose a base branch
from
aleaxis09:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
39cd3d2
Headers added
cristian4699 c25ab01
Headers added to __init__.py file
cristian4699 70d6f44
Change in pywebpush version dependecy
cristian4699 4b95d00
Version modified to 0.3.6
cristian4699 6a985d5
feat: Add default required headers for Apple push notifications
cristian4699 0edde16
tt default value modified
cristian4699 ed32855
Update README.md
aleaxis09 50aabc5
Update README.md
aleaxis09 c7f7ee7
Update README.md
aleaxis09 b6e760c
Update README.md
aleaxis09 8fb3407
Update README.md
aleaxis09 6e0e8e0
Update README.md
aleaxis09 c15ab16
Update README.md
aleaxis09 9638442
Update README.md
aleaxis09 7a9f101
Update README.md
aleaxis09 6601119
Update README.md
aleaxis09 5693476
Update README.md
aleaxis09 729352d
Update utils.py
aleaxis09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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` | ||
|
@@ -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 | ||
======= | ||
---- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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={}): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = {} | ||
|
||
|
@@ -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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well