From 6a1d9299f50b3b780dd355fb8ef9967026b4d58e Mon Sep 17 00:00:00 2001 From: murdle Date: Sun, 21 Dec 2025 16:23:31 +0200 Subject: [PATCH] fix notifs on ios --- assets/public/custom/pushMessages.js | 72 +++++++++++++++++----------- assets/public/index.html | 1 + 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/assets/public/custom/pushMessages.js b/assets/public/custom/pushMessages.js index cc96a1ed..9a8862a5 100644 --- a/assets/public/custom/pushMessages.js +++ b/assets/public/custom/pushMessages.js @@ -44,38 +44,52 @@ async function unregisterPush() { } async function registerPush(publicKey) { - checkServiceWorkerSupport(); + checkServiceWorkerSupport(); - const permission = await Notification.requestPermission(); - if (permission !== "granted") { - throw new Error("Notification permission not granted"); - } + if (window.IS_IOS && !window.IS_STANDALONE) { + throw new Error( + "On iOS, notifications require adding this site to the Home Screen." + ); + } - const registration = await navigator.serviceWorker.register("/assets/custom/serviceWorker.js"); + const permission = await Notification.requestPermission(); + if (permission !== "granted") { + throw new Error("Notification permission not granted"); + } - let subscription = await registration.pushManager.getSubscription(); - if (!subscription) { - const applicationServerKey = urlBase64ToUint8Array(publicKey); - subscription = await registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey - }); - } + const registration = await navigator.serviceWorker.register( + "/assets/custom/serviceWorker.js", + { scope: "/" } + ); - const response = await fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/v9/users/@me/devices`, { - method: "POST", - headers: { - "Content-Type": "application/json", - "Authorization": window.AUTH_TOKEN - }, - body: JSON.stringify({ - provider: "webpush", - webpush_subscription: subscription - }) - }); + let subscription = await registration.pushManager.getSubscription(); - if (!response.ok) { - const text = await response.text(); - throw new Error("Failed to register: ", text) - } + if (!subscription) { + const applicationServerKey = urlBase64ToUint8Array(publicKey); + + subscription = await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey + }); + } + + const response = await fetch( + `${window.GLOBAL_ENV.API_ENDPOINT}/v9/users/@me/devices`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: window.AUTH_TOKEN + }, + body: JSON.stringify({ + provider: "webpush", + subscription + }) + } + ); + + if (!response.ok) { + const text = await response.text(); + throw new Error(`Failed to register push: ${text}`); + } } \ No newline at end of file diff --git a/assets/public/index.html b/assets/public/index.html index 4b611c0c..a5626d7b 100644 --- a/assets/public/index.html +++ b/assets/public/index.html @@ -39,6 +39,7 @@ window.IS_IOS = /iPad|iPhone|iPod/.test(navigator.userAgent); window.IS_MOBILE = window.matchMedia("(pointer: coarse)").matches; + window.IS_STANDALONE = window.matchMedia("(display-mode: standalone)").matches || window.navigator.standalone === true; function protocolRelative(url) { if (!url) return "";