From f8caafb2a98de2a64f5759ac71b4debae9abf526 Mon Sep 17 00:00:00 2001 From: SDE Date: Sun, 5 May 2024 13:37:39 +0300 Subject: [PATCH] 0.12.47 fix push messages --- GeneralApp/funcs.py | 4 ++-- TWB/settings.py | 6 +++--- static/js/push/registerSw.js | 22 +++++++++++++++++++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/GeneralApp/funcs.py b/GeneralApp/funcs.py index d4370b4..2169cd2 100644 --- a/GeneralApp/funcs.py +++ b/GeneralApp/funcs.py @@ -69,7 +69,7 @@ def get_inter_http_respose(template_obj, context_Dict, request): title = getattr(context_Dict['page'], 'title', None) text = getattr(context_Dict['page'], 'description', None) - # if text and title and not request.user.is_anonymous: - # send_push(user=request.user, title=title, text=text) + if text and title and not request.user.is_anonymous: + send_push(user=request.user, title=title, text=text) return HttpResponse(template_obj.render(context_Dict, request)) \ No newline at end of file diff --git a/TWB/settings.py b/TWB/settings.py index dbe2f84..f32da5b 100644 --- a/TWB/settings.py +++ b/TWB/settings.py @@ -33,9 +33,9 @@ ALLOWED_HOSTS = ["*"] WEBPUSH_SETTINGS = { # "VAPID_PUBLIC_KEY": "BKS8byh3MucwCF2h06JY9oey1s1RYII09j-j3ehI3qTYhs965UHv0qNPl-jFjQBbIJCvjVXm9RW6t_oJJK8yMOk", # "VAPID_PRIVATE_KEY": "f5NMgOntBtRqsyeKwEzloK-051ggMnZGF_GFimERY0w", - "VAPID_PUBLIC_KEY": "BNf5y-99x5UNoaK98VAPKzZOevJHnak-LUmiQFu_r0i8tO3z7TPsto0DKHzNUO76ejNVWjmpL3UoavIsa3C3NEs", - "VAPID_PRIVATE_KEY": "4m8Z-bmT7ofb4bxM488eqFUksqnBUGzZh2rBrfSvPGo", - "VAPID_ADMIN_EMAIL": "admin@tripwb.com" + "VAPID_PUBLIC_KEY": "BBxRDxjgX-2DbZa3OLtxMuejTxaliMLd6QK6mG15a5oathxDtNcyioMMPMNTbljG-Hvc3eVQniJ_Ue6e1DN7VXM", + "VAPID_PRIVATE_KEY": "YruXqS1ZmxJB2heQU7orDojN-ZQHBSTxkBJ0gVrF0JU", + "VAPID_ADMIN_EMAIL": "tripwithbonus@gmail.com" } diff --git a/static/js/push/registerSw.js b/static/js/push/registerSw.js index 1220512..695e02c 100644 --- a/static/js/push/registerSw.js +++ b/static/js/push/registerSw.js @@ -30,6 +30,26 @@ const showNotAllowed = (message) => { button.setAttribute('disabled', 'true'); }; + + +// urlB64ToUint8Array is a magic function that will encode the base64 public key +// to Array buffer which is needed by the subscription option +function urlB64ToUint8Array2(base64String) { + const padding = '='.repeat((4 - (base64String.length % 4)) % 4) + const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/') + const rawData = atob(base64) + const outputArray = new Uint8Array(rawData.length) + for (let i = 0; i < rawData.length; ++i) { + outputArray[i] = rawData.charCodeAt(i) + } + return outputArray +} + + + + + + function urlB64ToUint8Array(base64String) { const padding = '='.repeat((4 - base64String.length % 4) % 4); const base64 = (base64String + padding) @@ -55,7 +75,7 @@ const subscribe = async (reg) => { const options = { userVisibleOnly: true, // if key exists, create applicationServerKey property - ...(key && {applicationServerKey: urlB64ToUint8Array(key)}) + ...(key && {applicationServerKey: urlB64ToUint8Array2(key)}) }; const sub = await reg.pushManager.subscribe(options);