From 23d51b70444704b809816af2ddde4053fa334518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Tue, 3 Jun 2025 16:38:20 +0200 Subject: [PATCH 1/2] This implementation makes the TTL configurable A TTL is required for Edge; without it, a 400 Bad Request error is returned. --- README.rst | 1 + push_notifications/conf/app.py | 6 +++++- push_notifications/conf/legacy.py | 4 ++++ push_notifications/settings.py | 1 + push_notifications/webpush.py | 1 + 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 81650929..69ec708d 100644 --- a/README.rst +++ b/README.rst @@ -130,6 +130,7 @@ For WNS, you need both the ``WNS_PACKAGE_SECURITY_KEY`` and the ``WNS_SECRET_KEY - ``WP_PRIVATE_KEY``: Absolute path to your private certificate file: os.path.join(BASE_DIR, "private_key.pem") - ``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:development@example.com"}. - ``WP_ERROR_TIMEOUT``: The timeout on WebPush POSTs. (Optional) +- ``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) For more information about how to configure WebPush, see `docs/WebPush `_. diff --git a/push_notifications/conf/app.py b/push_notifications/conf/app.py index a70aa1f5..5469e464 100644 --- a/push_notifications/conf/app.py +++ b/push_notifications/conf/app.py @@ -63,7 +63,7 @@ WNS_OPTIONAL_SETTINGS = ["WNS_ACCESS_URL"] WP_REQUIRED_SETTINGS = ["PRIVATE_KEY", "CLAIMS"] -WP_OPTIONAL_SETTINGS = ["ERROR_TIMEOUT", "POST_URL"] +WP_OPTIONAL_SETTINGS = ["ERROR_TIMEOUT", "POST_URL", "TTL"] class AppConfig(BaseConfig): @@ -218,6 +218,7 @@ def _validate_wp_config(self, application_id, application_config): "EDGE": "https://wns2-par02p.notify.windows.com/w", "FIREFOX": "https://updates.push.services.mozilla.com/wpush/v2", }) + application_config.setdefault("TTL", 300) def _validate_allowed_settings(self, application_id, application_config, allowed_settings): """Confirm only allowed settings are present.""" @@ -349,3 +350,6 @@ def get_wp_private_key(self, application_id=None): def get_wp_claims(self, application_id=None): return self._get_application_settings(application_id, "WP", "CLAIMS") + + def get_wp_ttl(self, application_id=None): + return self._get_application_settings(application_id, "WP", "TTL") diff --git a/push_notifications/conf/legacy.py b/push_notifications/conf/legacy.py index 93a64cab..d9058af7 100644 --- a/push_notifications/conf/legacy.py +++ b/push_notifications/conf/legacy.py @@ -128,3 +128,7 @@ def get_wp_private_key(self, application_id=None): def get_wp_claims(self, application_id=None): msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages" return self._get_application_settings(application_id, "WP_CLAIMS", msg) + + def get_wp_ttl(self, application_id=None): + msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to set a ttl (time to live)" + return self._get_application_settings(application_id, "WP_TTL", msg) diff --git a/push_notifications/settings.py b/push_notifications/settings.py index 5fba8b33..881347e9 100644 --- a/push_notifications/settings.py +++ b/push_notifications/settings.py @@ -41,6 +41,7 @@ PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_PRIVATE_KEY", None) PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_CLAIMS", None) PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_ERROR_TIMEOUT", None) +PUSH_NOTIFICATIONS_SETTINGS.setdefault("WP_TTL", 300) # User model PUSH_NOTIFICATIONS_SETTINGS.setdefault("USER_MODEL", settings.AUTH_USER_MODEL) diff --git a/push_notifications/webpush.py b/push_notifications/webpush.py index de379431..88a59034 100644 --- a/push_notifications/webpush.py +++ b/push_notifications/webpush.py @@ -37,6 +37,7 @@ def webpush_send_message(device, message, **kwargs): data=message, vapid_private_key=get_manager().get_wp_private_key(device.application_id), vapid_claims=get_manager().get_wp_claims(device.application_id).copy(), + ttl=get_manager().get_wp_ttl(device.application_id), **kwargs ) if response.ok: From a184e854b2e8e553c264caeadda50bf5c5c40721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Tue, 3 Jun 2025 17:19:27 +0200 Subject: [PATCH 2/2] Improve readme file --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 69ec708d..7586e34f 100644 --- a/README.rst +++ b/README.rst @@ -130,7 +130,7 @@ For WNS, you need both the ``WNS_PACKAGE_SECURITY_KEY`` and the ``WNS_SECRET_KEY - ``WP_PRIVATE_KEY``: Absolute path to your private certificate file: os.path.join(BASE_DIR, "private_key.pem") - ``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:development@example.com"}. - ``WP_ERROR_TIMEOUT``: The timeout on WebPush POSTs. (Optional) -- ``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) +- ``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, default value is 5 minutes, 300 seconds) For more information about how to configure WebPush, see `docs/WebPush `_.