Add push notifications: rest-timer alert + workout-day reminder

Web Push (VAPID) backend: keys auto-generated on first run, per-user
subscriptions, one new dependency (web-push). The rest timer schedules
a server-side push on start/extend and cancels it on skip or on-screen
completion, so it only actually fires if the tab got suspended before
finishing. A per-minute check sends an optional daily reminder on days
with a planned routine that hasn't been logged yet.

Opt in per-profile in Settings — requires a signed-in passkey profile.
This commit is contained in:
Duarte Santos committed 2026-07-21 11:07:10 +02:00
1 parent 84c59129e3
commit 4c2b6344f7
11 files changed
+448 -7

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
// Web Push subscribe/unsubscribe — requires a signed-in profile (subscriptions are stored
// server-side per user, same as everything else under /api).
import { api } from './api.js'
export const pushSupported = () => 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window
export const pushPermission = () => (pushSupported() ? Notification.permission : 'unsupported')
const urlBase64ToUint8Array = b64 => {
const padded = (b64 + '='.repeat((4 - b64.length % 4) % 4)).replace(/-/g, '+').replace(/_/g, '/')
const raw = atob(padded)
return Uint8Array.from([...raw].map(c => c.charCodeAt(0)))
}
export async function enablePush() {
if (!pushSupported()) throw new Error('Push notifications are not supported in this browser')
const perm = await Notification.requestPermission()
if (perm !== 'granted') throw new Error('Notifications permission was not granted')
const reg = await navigator.serviceWorker.ready
const { key } = await api('/api/push/public-key')
const subscription = await reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlBase64ToUint8Array(key) })
await api('/api/push/subscribe', { method: 'POST', body: JSON.stringify({ subscription: subscription.toJSON() }) })
}
export async function disablePush() {
if (!pushSupported()) return
const reg = await navigator.serviceWorker.ready
const sub = await reg.pushManager.getSubscription()
if (!sub) return
await sub.unsubscribe()
await api('/api/push/unsubscribe', { method: 'POST', body: JSON.stringify({ endpoint: sub.endpoint }) }).catch(() => {})
}
export const sendTestPush = () => api('/api/push/test', { method: 'POST', body: '{}' })