export async function setupNotifications(owner, { notice, sync, buttonId = "enable-notifications", statusId = "notification-status", ownerKey = "dm-todo-push-owner", ownerMessage = "TODO_OWNER", endpoint = "/api/notifications" }) { const button = document.getElementById(buttonId), status = document.getElementById(statusId); if (!button || !status) return; const supported = "serviceWorker" in navigator && "PushManager" in window && "Notification" in window; if (!supported) { button.disabled = true; status.textContent = "notifications are unavailable in this browser. on iphone or ipad, install this app on your home screen first."; return; } let registration; try { registration = await navigator.serviceWorker.ready; } catch { return; } const setOwner = () => { registration.active?.postMessage({ type: ownerMessage, owner }); }; setOwner(); navigator.serviceWorker.addEventListener("controllerchange", setOwner); let subscription = await registration.pushManager.getSubscription(); if (subscription && localStorage.getItem(ownerKey) !== owner) { await subscription.unsubscribe(); subscription = null; localStorage.removeItem(ownerKey); } const enabled = () => { button.textContent = subscription ? "disable due-date notifications" : "enable due-date notifications"; status.textContent = subscription ? "enabled \xB7 reminders from 9 a.m. in your timezone." : "reminders at 9 a.m. in your timezone."; }; enabled(); if (!owner) { button.disabled = true; status.textContent = "sign in to enable due-date notifications."; return; } async function register(sub) { const response = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ owner, subscription: sub.toJSON(), locale: document.documentElement.lang, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone }) }); if (!response.ok) throw Error( "reminders could not be enabled. reconnect and sign in, then try again." ); localStorage.setItem(ownerKey, owner); } if (subscription && navigator.onLine) register(subscription).catch(() => { status.textContent = "reconnect and sign in to refresh reminders."; }); button.addEventListener("click", async () => { if (!navigator.onLine) { notice("connect to the internet to change notifications."); return; } button.disabled = true; try { if (subscription) { const response = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ owner, remove: true, endpoint: subscription.endpoint }) }); if (!response.ok) throw Error("could not disable notifications. try again."); await subscription.unsubscribe(); subscription = null; localStorage.removeItem(ownerKey); } else { const permission = await Notification.requestPermission(); if (permission !== "granted") { status.textContent = "allow notifications in your browser settings to enable reminders."; return; } const response = await fetch(endpoint); if (!response.ok) throw Error("sign in again to enable notifications."); const config = await response.json(); if (config.owner !== owner || !config.publicKey) throw Error("notifications are unavailable. try again."); const bytes = Uint8Array.from( atob(config.publicKey.replaceAll("-", "+").replaceAll("_", "/")), (c) => c.charCodeAt(0) ); subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: bytes }); try { await register(subscription); } catch (error) { await subscription.unsubscribe(); subscription = null; throw error; } setOwner(); await sync(); } enabled(); } catch (error) { notice(error.message); } finally { button.disabled = false; } }); }