Files
takeyourmeds/public/lib/pwa.js
T
Frank Schwenk 1cada42370 feat: replace Web Push with ntfy for medication reminders
Single notification path via ntfy HTTP publish for reliable Android delivery;
remove VAPID, push subscriptions, and SW push handlers. PWA settings show
topic subscribe link; humor texts and deep-link actions unchanged.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 10:42:58 +02:00

49 lines
1.4 KiB
JavaScript

let deferredInstallPrompt = null;
function isStandalone() {
return window.matchMedia("(display-mode: standalone)").matches || window.navigator.standalone === true;
}
export function wirePwaInstall({ sectionId = "pwaInstallSection", buttonId = "installPwa" } = {}) {
const section = document.getElementById(sectionId);
const button = document.getElementById(buttonId);
if (!section || !button) return;
const hide = () => { section.hidden = true; };
if (isStandalone()) { hide(); return; }
hide();
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
deferredInstallPrompt = e;
section.hidden = false;
});
window.addEventListener("appinstalled", () => {
deferredInstallPrompt = null;
hide();
});
button.addEventListener("click", async () => {
if (!deferredInstallPrompt) return;
deferredInstallPrompt.prompt();
await deferredInstallPrompt.userChoice;
deferredInstallPrompt = null;
hide();
});
}
export async function registerPwa() {
if (!("serviceWorker" in navigator)) return;
try {
await navigator.serviceWorker.register("/sw.js");
if ("sync" in ServiceWorkerRegistration.prototype) {
navigator.serviceWorker.ready.then((reg) => {
reg.sync?.register("sync-logs").catch(() => {});
});
}
} catch (e) {
console.error("SW registration failed:", e);
}
}