mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
feat(notifications): strengthen the Web Push path for PWA-only self-hosters
Self-hosters without an Apple Developer account rely on Web Push to the installed PWA — there is no Live Activity or lock-screen widget to drive. Make that path carry the dose lifecycle on its own: - Stable per-slot tag. The medication dose-due reminder now sets a `med:<id>:<iso-slot>` Web Push tag (medicationDoseTag), so a re-fired reminder for the same slot coalesces instead of stacking, and a later clear can target it. - Clear-on-taken. When a dose is marked taken or skipped, push a `type:"clear"` notification reusing that slot tag. The service worker closes the matching pending reminder and refreshes the badge — the PWA equivalent of ending a Live Activity. A snooze leaves the dose outstanding and clears nothing. Wired into the single + bulk intake routes alongside the existing APNs cross-device sync. - App badge. The dispatcher carries a server-authoritative outstanding- dose count; the SW reflects it via the Badging API (setAppBadge/clearAppBadge), feature-detected and a no-op where unsupported. - The SW push handler honours requireInteraction for urgent events and renotify so a re-fired reminder re-alerts. Dose-due, preventive-care, and low-stock reminders already cascade to Web Push when no APNs channel exists; tests pin that an APNs-less instance still delivers.
This commit is contained in:
1 parent
0163f159d5
commit
604aec631f
11 files changed
+666
-10
No files matched your search
+62
-8
@@ -36,7 +36,7 @@ try {
|
||||
// v1.4.38.4 → v1.4.42. Do not hand-edit; bump `package.json` and rebuild.
|
||||
const CACHE_VERSION =
|
||||
(typeof self !== "undefined" && self.__APP_VERSION__) ||
|
||||
/* @sw-version-fallback */ "v1.18.2";
|
||||
/* @sw-version-fallback */ "v1.18.4";
|
||||
const STATIC_CACHE = `healthlog-static-${CACHE_VERSION}`;
|
||||
const PAGE_CACHE = `healthlog-pages-${CACHE_VERSION}`;
|
||||
const MAX_STATIC_ENTRIES = 150;
|
||||
@@ -216,6 +216,27 @@ async function trimCache(cacheName, maxEntries) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── App badge helper (PWA Badging API) ───────────────────────────────────────
|
||||
// v1.18.4 — reflect the server-authoritative outstanding-dose count on the
|
||||
// installed PWA icon. Feature-detected + best-effort: unsupported engines
|
||||
// (most desktop Firefox, older Safari) silently no-op. A count of 0 clears the
|
||||
// badge; a positive count sets it. `undefined`/non-number leaves it untouched.
|
||||
function applyAppBadge(count) {
|
||||
if (typeof count !== "number" || !isFinite(count)) return;
|
||||
try {
|
||||
if (count > 0) {
|
||||
if (typeof self.navigator?.setAppBadge === "function") {
|
||||
self.navigator.setAppBadge(count);
|
||||
}
|
||||
} else if (typeof self.navigator?.clearAppBadge === "function") {
|
||||
self.navigator.clearAppBadge();
|
||||
}
|
||||
} catch {
|
||||
// Badging API can reject (permission / unsupported) — never let it
|
||||
// break the push handler.
|
||||
}
|
||||
}
|
||||
|
||||
// ── Push Notifications ───────────────────────────────────────────────────────
|
||||
self.addEventListener("push", (event) => {
|
||||
if (!event.data) return;
|
||||
@@ -227,21 +248,54 @@ self.addEventListener("push", (event) => {
|
||||
payload = { title: "HealthLog", body: event.data.text() };
|
||||
}
|
||||
|
||||
// v1.18.4 — `type:"clear"` is the PWA equivalent of ending an iOS Live
|
||||
// Activity: the server sends it when a dose is logged so the still-pending
|
||||
// dose-due reminder for that slot is closed here (matched on its stable
|
||||
// `tag`), and the app badge re-reflects the outstanding-dose count. No new
|
||||
// notification is shown.
|
||||
if (payload && payload.type === "clear") {
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
const tagToClear = payload.tag;
|
||||
if (tagToClear) {
|
||||
const matches = await self.registration.getNotifications({
|
||||
tag: tagToClear,
|
||||
});
|
||||
for (const n of matches) n.close();
|
||||
}
|
||||
applyAppBadge(payload.badge);
|
||||
})(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
title = "HealthLog",
|
||||
body = "",
|
||||
tag = "default",
|
||||
url = "/",
|
||||
badge,
|
||||
requireInteraction = false,
|
||||
} = payload;
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(title, {
|
||||
body,
|
||||
tag,
|
||||
icon: "/logo-192.png",
|
||||
badge: "/logo-192.png",
|
||||
data: { url },
|
||||
}),
|
||||
Promise.all([
|
||||
self.registration.showNotification(title, {
|
||||
body,
|
||||
tag,
|
||||
// `renotify` lets a re-fired reminder for the same `tag` re-alert the
|
||||
// user instead of silently replacing the existing notification.
|
||||
renotify: true,
|
||||
// v1.18.4 — urgent events (the web-push sender sets this) keep the
|
||||
// notification on screen until the user acts, where the browser
|
||||
// honours it.
|
||||
requireInteraction: requireInteraction === true,
|
||||
icon: "/logo-192.png",
|
||||
badge: "/logo-192.png",
|
||||
data: { url },
|
||||
}),
|
||||
Promise.resolve(applyAppBadge(badge)),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user