fix notifs on ios

This commit is contained in:
murdle 2025-12-21 16:23:31 +02:00
parent a7b349241c
commit 6a1d9299f5
2 changed files with 44 additions and 29 deletions

View File

@ -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}`);
}
}

View File

@ -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 "";