Skip to content

Commit 23d51b7

Browse files
committed
This implementation makes the TTL configurable A TTL is required for Edge; without it, a 400 Bad Request error is returned.
1 parent 16d43c2 commit 23d51b7

File tree

5 files changed

+12
-1
lines changed

5 files changed

+12
-1
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ For WNS, you need both the ``WNS_PACKAGE_SECURITY_KEY`` and the ``WNS_SECRET_KEY
130130
- ``WP_PRIVATE_KEY``: Absolute path to your private certificate file: os.path.join(BASE_DIR, "private_key.pem")
131131
- ``WP_CLAIMS``: Dictionary with default value for the sub, (subject), sent to the webpush service, This would be used by the service if they needed to reach out to you (the sender). Could be a url or mailto e.g. {'sub': "mailto:[email protected]"}.
132132
- ``WP_ERROR_TIMEOUT``: The timeout on WebPush POSTs. (Optional)
133+
- ``WP_TTL``: Time to live: The maximum amount of time (in seconds) a push message should be kept in the messaging service's queue before it expires, if it hasn’t been delivered to the device.. (Optional)
133134

134135
For more information about how to configure WebPush, see `docs/WebPush <https://github.com/jazzband/django-push-notifications/blob/master/docs/WebPush.rst>`_.
135136

push_notifications/conf/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
WNS_OPTIONAL_SETTINGS = ["WNS_ACCESS_URL"]
6464

6565
WP_REQUIRED_SETTINGS = ["PRIVATE_KEY", "CLAIMS"]
66-
WP_OPTIONAL_SETTINGS = ["ERROR_TIMEOUT", "POST_URL"]
66+
WP_OPTIONAL_SETTINGS = ["ERROR_TIMEOUT", "POST_URL", "TTL"]
6767

6868

6969
class AppConfig(BaseConfig):
@@ -218,6 +218,7 @@ def _validate_wp_config(self, application_id, application_config):
218218
"EDGE": "https://wns2-par02p.notify.windows.com/w",
219219
"FIREFOX": "https://updates.push.services.mozilla.com/wpush/v2",
220220
})
221+
application_config.setdefault("TTL", 300)
221222

222223
def _validate_allowed_settings(self, application_id, application_config, allowed_settings):
223224
"""Confirm only allowed settings are present."""
@@ -349,3 +350,6 @@ def get_wp_private_key(self, application_id=None):
349350

350351
def get_wp_claims(self, application_id=None):
351352
return self._get_application_settings(application_id, "WP", "CLAIMS")
353+
354+
def get_wp_ttl(self, application_id=None):
355+
return self._get_application_settings(application_id, "WP", "TTL")

push_notifications/conf/legacy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,7 @@ def get_wp_private_key(self, application_id=None):
128128
def get_wp_claims(self, application_id=None):
129129
msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
130130
return self._get_application_settings(application_id, "WP_CLAIMS", msg)
131+
132+
def get_wp_ttl(self, application_id=None):
133+
msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to set a ttl (time to live)"
134+
return self._get_application_settings(application_id, "WP_TTL", msg)

push_notifications/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_PRIVATE_KEY", None)
4242
PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_CLAIMS", None)
4343
PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_ERROR_TIMEOUT", None)
44+
PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_TTL", 300)
4445

4546
# User model
4647
PUSH_NOTIFICATIONS_SETTINGS.setdefault("USER_MODEL", settings.AUTH_USER_MODEL)

push_notifications/webpush.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def webpush_send_message(device, message, **kwargs):
3737
data=message,
3838
vapid_private_key=get_manager().get_wp_private_key(device.application_id),
3939
vapid_claims=get_manager().get_wp_claims(device.application_id).copy(),
40+
ttl=get_manager().get_wp_ttl(device.application_id),
4041
**kwargs
4142
)
4243
if response.ok:

0 commit comments

Comments
 (0)