Add push reminders for sleep, pee, poo and meals

A closed PWA has no timers, so reminders are evaluated on the server: the
event log is already there (clients sync on every mutation), and a ticker
re-checks each enabled rule once a minute and pushes the ones that are due.

Two rule shapes. "sleep" measures from the last sleep-end and fires only
while the puppy is awake. "pee"/"poo"/"eat" measure from the newest event of
that type and stay quiet while the puppy is asleep — otherwise they nag all
night, and suppressing them means an overdue rule instead fires promptly on
waking, which is when it actually matters. Sleep state is derived exactly the
way currentSleepState() does in app.js, tie-break included, so both sides
always agree. Rules read the event's own timestamp rather than when it synced,
so a pee logged offline at 03:10 cancels the reminder retroactively.

Every push carries a tag, so a repeat replaces the previous notification
instead of stacking another one on the lock screen. last_fired is server-owned
and not writable by a client, so a stale device can't force a re-fire.

Web Push is implemented directly rather than pulled in as a dependency: RFC
8291 encryption in the RFC 8188 aes128gcm coding with an RFC 8292 VAPID token,
stdlib only, checked against the RFC 8291 test vector. The key is generated
into vapid.json beside the DB or supplied via -vapid-key; without one the
server logs a warning, skips registering the routes, and the client hides the
UI. Subscriptions a push service reports as 404/410 are dropped.

PNG icons are added because iOS gates push on a Home Screen install and
rejects SVG for apple-touch-icon, and Android has no notification icon
without them.
This commit is contained in:
Alexander Heldt
2026-08-20 17:19:18 +00:00
parent 93d6ea27a7
commit 51d015c231
17 changed files with 1918 additions and 11 deletions
+46
View File
@@ -13,6 +13,9 @@ const ASSETS = [
"./app.js",
"./manifest.json",
"./icon.svg",
"./icon-180.png",
"./icon-192.png",
"./icon-512.png",
"./changelog.json",
];
@@ -92,3 +95,46 @@ self.addEventListener("fetch", (event) => {
})
);
});
// ---------- push reminders ----------
// The server evaluates reminder rules and pushes the ones that come due (see
// server/reminders.go). Subscriptions are userVisibleOnly, so every push must
// result in a notification — there is no silent path to fall back on.
self.addEventListener("push", (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch (err) {
data = {};
}
// The tag is what makes a repeat of the same reminder replace the previous
// notification instead of stacking another one on the lock screen, and
// renotify:false lets that replacement happen without re-alerting.
event.waitUntil(
self.registration.showNotification(data.title || "Puppy Tracker", {
body: data.body || "",
tag: data.tag || "reminder",
renotify: false,
icon: "./icon-192.png",
badge: "./icon-192.png",
data: { url: data.url || "./" },
})
);
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const url = (event.notification.data && event.notification.data.url) || "./";
// Prefer focusing a tab the app is already open in — opening a second window
// onto the same PWA is disorienting and loses whatever was on screen.
event.waitUntil(
self.clients
.matchAll({ type: "window", includeUncontrolled: true })
.then((clients) => {
for (const client of clients) {
if ("focus" in client) return client.focus();
}
return self.clients.openWindow(url);
})
);
});