From 39cd3d2d565ad692dd69c8ecc0156f7673cbbf7c Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 14 Apr 2023 10:45:18 -0500 Subject: [PATCH 01/18] Headers added --- webpush/utils.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/webpush/utils.py b/webpush/utils.py index 1a80e76..d6f56b4 100644 --- a/webpush/utils.py +++ b/webpush/utils.py @@ -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={}): 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 = {} @@ -43,7 +43,7 @@ def _send_notification(subscription, payload, ttl): } 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. From c25ab01199c7febb2e9823383ef837a76e7853a7 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 14 Apr 2023 10:55:37 -0500 Subject: [PATCH 02/18] Headers added to __init__.py file --- webpush/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/webpush/__init__.py b/webpush/__init__.py index cb17506..fa5ff76 100644 --- a/webpush/__init__.py +++ b/webpush/__init__.py @@ -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={}): 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) From 70d6f44544b1fb9ebff7a9c63ffd3890c1707998 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 14 Apr 2023 11:00:15 -0500 Subject: [PATCH 03/18] Change in pywebpush version dependecy --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 08840b2..d953d36 100644 --- a/setup.py +++ b/setup.py @@ -34,6 +34,6 @@ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', ], install_requires=[ - 'pywebpush==1.9.4' + 'pywebpush>=1.9.4' ] ) From 4b95d00edbcf1a1b6dadf02a446e3b6439ce6002 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 14 Apr 2023 12:26:27 -0500 Subject: [PATCH 04/18] Version modified to 0.3.6 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d953d36..819ca6f 100644 --- a/setup.py +++ b/setup.py @@ -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', From 6a985d5461c7d3f5b7d12d20544b42f7200d4c82 Mon Sep 17 00:00:00 2001 From: Cristian Date: Mon, 15 May 2023 17:06:56 -0500 Subject: [PATCH 05/18] feat: Add default required headers for Apple push notifications --- webpush/utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/webpush/utils.py b/webpush/utils.py index d6f56b4..f7c6294 100644 --- a/webpush/utils.py +++ b/webpush/utils.py @@ -42,6 +42,19 @@ def _send_notification(subscription, payload, ttl, headers={}): 'vapid_claims': {"sub": "mailto:{}".format(vapid_admin_email)} } + endpoint = subscription_data.get("endpoint") + if endpoint.startswith("https://web.push.apple.com"): + """ + ttl, topic, urgency are required headers for web push notifications with Apple + 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", "0")) + headers['topic'] = str(headers.get("topic", "10")) + headers['urgency'] = headers.get("urgency", "normal") + + try: req = webpush(subscription_info=subscription_data, data=payload, ttl=ttl, headers=headers, **vapid_data) return req From 0edde16d62eb5fc9c027952acd4367ed7acd6f42 Mon Sep 17 00:00:00 2001 From: Cristian Date: Mon, 15 May 2023 17:10:04 -0500 Subject: [PATCH 06/18] tt default value modified --- webpush/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpush/utils.py b/webpush/utils.py index f7c6294..ef822be 100644 --- a/webpush/utils.py +++ b/webpush/utils.py @@ -50,7 +50,7 @@ def _send_notification(subscription, payload, ttl, headers={}): https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_safari_and_other_browsers#3994592 """ - headers['ttl'] = str(headers.get("ttl", "0")) + headers['ttl'] = str(headers.get("ttl", ttl)) headers['topic'] = str(headers.get("topic", "10")) headers['urgency'] = headers.get("urgency", "normal") From ed3285535321d57b53836cba5817e68001dff78b Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 15 May 2023 18:21:43 -0500 Subject: [PATCH 07/18] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 089557b..b0497d3 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,14 @@ So in order to send notification, see below. ![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: + + headers = { "topic" : "0", "urgency" = "normal" } + send_user_notification(user=user, payload=payload, ttl=1000, headers=headers) + + You can find more information at the following link: + https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_safari_and_other_browsers#3994592 + License ======= ---- From 50aabc53cce0b274104b21d54d0e493eb2328397 Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 15 May 2023 18:26:00 -0500 Subject: [PATCH 08/18] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b0497d3..4d9c5a5 100644 --- a/README.md +++ b/README.md @@ -201,11 +201,15 @@ 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: headers = { "topic" : "0", "urgency" = "normal" } + send_user_notification(user=user, payload=payload, ttl=1000, headers=headers) You can find more information at the following link: From c7f7ee7de2ceca2a855c06039c755c1f1a6e5183 Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 15 May 2023 18:27:20 -0500 Subject: [PATCH 09/18] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 4d9c5a5..af4927f 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,6 @@ So in order to send notification, see below. ![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: From b6e760ceff3886b771ca8cff7b5cab7506125342 Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 09:24:12 -0500 Subject: [PATCH 10/18] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index af4927f..9538b82 100644 --- a/README.md +++ b/README.md @@ -206,14 +206,16 @@ So in order to send notification, see below. - To send push notifications to the safari browser, apple allows you to pass custom headers, you can send them as follows: - + + ```python headers = { "topic" : "0", "urgency" = "normal" } send_user_notification(user=user, payload=payload, ttl=1000, headers=headers) You can find more information at the following link: https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_safari_and_other_browsers#3994592 - + ``` + License ======= ---- From 8fb34074db0e3333ff5af998329f292b79af2a4c Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 09:30:38 -0500 Subject: [PATCH 11/18] Update README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9538b82..9e37e19 100644 --- a/README.md +++ b/README.md @@ -208,12 +208,16 @@ So in order to send notification, see below. - 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 + + user = request.user headers = { "topic" : "0", "urgency" = "normal" } + payload = {"head": "Welcome!", "body": "Hello World"} send_user_notification(user=user, payload=payload, ttl=1000, headers=headers) - You can find more information at the following link: - https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_safari_and_other_browsers#3994592 + + 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 From 6e0e8e04b1b0d6ab6594ec5199fdb66e56f005d0 Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 09:37:35 -0500 Subject: [PATCH 12/18] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e37e19..bf59fdf 100644 --- a/README.md +++ b/README.md @@ -215,11 +215,12 @@ So in order to send notification, see below. 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. ``` + 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 ======= ---- From c15ab168bdb503cc8c44cfed397c3cc86db1379b Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 10:27:01 -0500 Subject: [PATCH 13/18] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index bf59fdf..68c00e1 100644 --- a/README.md +++ b/README.md @@ -210,8 +210,7 @@ So in order to send notification, see below. ```python from webpush import send_user_notification - user = request.user - headers = { "topic" : "0", "urgency" = "normal" } + headers = { "topic" : "0", "urgency" : "normal" } payload = {"head": "Welcome!", "body": "Hello World"} send_user_notification(user=user, payload=payload, ttl=1000, headers=headers) From 9638442b630c8d3a7af0bba1d4cce92ceaf94558 Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 10:39:47 -0500 Subject: [PATCH 14/18] Update README.md --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 68c00e1..b88edba 100644 --- a/README.md +++ b/README.md @@ -210,16 +210,23 @@ So in order to send notification, see below. ```python from webpush import send_user_notification - headers = { "topic" : "0", "urgency" : "normal" } + headers = {"topic": "0", "urgency": "normal"} payload = {"head": "Welcome!", "body": "Hello World"} send_user_notification(user=user, payload=payload, ttl=1000, headers=headers) ``` + To send push notifications to the safari browser, write the email vapid as follows: + + ```python + VAPID_ADMIN_EMAIL='email@example.com' + ``` + 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 ======= ---- From 7a9f101bf534507c45811f2d9aa6172d8b58103d Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 10:41:15 -0500 Subject: [PATCH 15/18] Update README.md --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b88edba..85a0ae9 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,13 @@ WEBPUSH_SETTINGS = { ``` **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 send push notifications to the safari browser, write the email vapid as follows: + + ```python + VAPID_ADMIN_EMAIL='email@example.com' + ``` + + > **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` @@ -217,12 +224,6 @@ So in order to send notification, see below. ``` - To send push notifications to the safari browser, write the email vapid as follows: - - ```python - VAPID_ADMIN_EMAIL='email@example.com' - ``` - 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. From 6601119c59d0831c02b09884b5de2e184ef787a0 Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 10:42:50 -0500 Subject: [PATCH 16/18] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 85a0ae9..b562070 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,11 @@ WEBPUSH_SETTINGS = { ``` **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 send push notifications to the safari browser, write the email vapid as follows: - - ```python - VAPID_ADMIN_EMAIL='email@example.com' - ``` +To send push notifications to the safari browser, write the email vapid as follows: + +```python +VAPID_ADMIN_EMAIL='email@example.com' +``` > **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.** From 569347643a8f495a47a7e73602922d1c1d147113 Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 11:01:10 -0500 Subject: [PATCH 17/18] Update README.md --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index b562070..0481356 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,7 @@ WEBPUSH_SETTINGS = { ``` **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 send push notifications to the safari browser, write the email vapid as follows: - -```python -VAPID_ADMIN_EMAIL='email@example.com' -``` +To send push notifications to the safari browser, be sure to write the vapid email as `'email@example.com'` instead of `'mailto:email@example.com'` > **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.** From 729352d4f98f04b38e7f4632688036876a71115c Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 16 May 2023 11:14:16 -0500 Subject: [PATCH 18/18] Update utils.py --- webpush/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpush/utils.py b/webpush/utils.py index ef822be..4233a87 100644 --- a/webpush/utils.py +++ b/webpush/utils.py @@ -45,7 +45,7 @@ def _send_notification(subscription, payload, ttl, headers={}): endpoint = subscription_data.get("endpoint") if endpoint.startswith("https://web.push.apple.com"): """ - ttl, topic, urgency are required headers for web push notifications with Apple + 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 """