Skip to content

Mobile polish, phase 3: Web Push when a timer phase ends + app badge - #26

Merged
antonsxh merged 2 commits into
mainfrom
mobile-push
Nov 22, 2026
Merged

antonsxh merged 2 commits into
mainfrom
mobile-push

Conversation

@antonsxh

@antonsxh antonsxh commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

The timer runs on the server, but on the iPhone you only noticed a phase end while the app was open. Since iOS 16.4, home-screen web apps can receive Web Push. This PR adds:

  • Push notifications when a timer phase ends, to every device that turned them on;
  • an app icon badge with today's open tasks.

How it works

  • Keys: VAPID keys are generated on first use and stored in a new single-row table push_config. There is no new required env var.
    • The VAPID subject is the app's origin, taken from the subscribe request (adapter-node derives it from ORIGIN).
  • Subscriptions: push_subscriptions, one per device. Re-subscribing replaces it, and it stores the device's language for the auto setting. revokeDevice() deletes the device's subscriptions.
  • Crypto (server/push-crypto.ts): VAPID ES256 and RFC 8291 aes128gcm, written with WebCrypto. No new dependency. It reproduces the RFC 8291 example byte for byte, and its output decrypts the same way as web-push@3.6.7's.
  • SSRF guard (isAllowedPushEndpoint in #lib/push.ts):
    • only https, the default port and no credentials;
    • only these hosts: web.push.apple.com, *.push.apple.com, fcm.googleapis.com, (*.)push.services.mozilla.com, *.notify.windows.com;
    • checked on subscribe and again before every send, with redirects disabled;
    • for e2e only, the optional LIBE_PUSH_TEST_ENDPOINT_ORIGIN allows a loopback origin.
  • Scheduler (server/push-scheduler.ts):
    • one setTimeout, re-armed from every timer snapshot the server broadcasts, so every change path is covered; it also re-arms from the DB on startup (init hook);
    • pomodoro phase ends are published from the server's rollover after the transaction commits;
    • each event has a key, and an atomic check-and-set in the DB on that key sends it at most once, including across restarts;
    • sending never blocks a timer request.
  • Push headers: TTL 300 s, Urgency: high, Topic: libe-timer. A 404/410 deletes the subscription; other failures are recorded and logged once, with no retries.
  • Service worker: always shows a notification, since iOS revokes subscriptions after silent pushes. It uses a tag to replace an older one, and tapping it focuses or opens /focus. It also updates the badge from the payload and re-subscribes on pushsubscriptionchange. Caching rules are unchanged.
  • Settings → Notifications: shows a short message instead of the toggle when push isn't supported, when the page isn't installed on the home screen (iOS), or when notifications are blocked. Otherwise it has Turn on (the permission is requested inside the tap) and, once on, Send test and Turn off. Hidden in the Tauri app.
  • Badge: openToday comes from the root layout load and is set only while this device has notifications on, because iOS ties badges to notification permission.

Notification texts (body = the task title)

Event English German
Countdown reached its planned time Time’s up Zeit ist um
Break started automatically Break time · 5 min Pause · 5 Min.
Long break started automatically Long break · 15 min Lange Pause · 15 Min.
Round done, break waits Round done — start your break Runde geschafft – starte deine Pause
Next round started automatically Break over — back to focus Pause vorbei – zurück zum Fokus
Next round waits Break over — start the next round Pause vorbei – starte die nächste Runde

Migration: 0007_web_push.

Screenshots

iPhone 390×844 (Chromium emulation). Headless Chromium has no push service, so the "off" and "on" states stub the permission and PushManager; the section and the API are real.

Before Off Not installed (Safari tab) On Blocked

Desktop: after-settings-desktop.png

Tests

  • Unit: allowlist, body validation, nextPushEvent (countdown and pomodoro sequences, pause shifts, idempotency keys), texts, notificationsState, and RFC 8291 encryption and VAPID. bun run test reports 453 pass.
  • e2e push.spec.ts, using a local fake push endpoint that decrypts each payload with the test's own keys:
    • exactly one countdown push, even after a recompute, with title, body, URL and VAPID header checked;
    • a 410 deletes the subscription;
    • revoking a device deletes its subscriptions;
    • an endpoint not on the allowlist gets a 400.
  • Rule 5: removing the send-once guard, the 410 cleanup or the delete-on-revoke each made the matching test fail.
  • Results: check 0 errors / 0 warnings, lint clean, build OK (also without .env). test:e2e 92 passed, 6 skipped. test:e2e:mobile 48 passed, 8 skipped.
  • Review fix: a timer event is now claimed even when nobody has subscribed. Before, a device that subscribed later got the stale event on the next timer change.
  • Not tested: real delivery to an iPhone or iPad (permission prompt, badge, tag replacement), and acceptance by the live FCM, Mozilla or WNS services.

🤖 Generated with Claude Code

antonsxh and others added 2 commits November 22, 2026 16:10
- Push notifications when a focus countdown reaches its target or a pomodoro
  phase ends (break started / waiting, back to focus / next round waiting),
  to every device that opted in; nothing for stopwatches, pauses or phases
  the user ends by hand.
- VAPID keys generated on first use and stored in the new single-row
  push_config table; push_subscriptions (one per device, deleted on revoke);
  migration 0007_web_push. No new required env var.
- Hand-written WebCrypto VAPID (ES256) + RFC 8291 aes128gcm (matches the RFC
  example byte for byte); endpoint allowlist of known push services as SSRF
  guard; 404/410 removes a subscription, other failures are recorded and logged.
- Scheduler: one setTimeout re-armed from every published timer snapshot;
  pomodoro rollovers publish their phase end after commit; one push per event
  key (DB compare-and-set), also across restarts.
- API /api/push (GET/POST/DELETE) and /api/push/test; service worker push,
  notificationclick and pushsubscriptionchange handlers.
- Settings → Notifications (install hint on iOS Safari, denied, off, on with
  Send test / Turn off; hidden in the desktop app); app icon badge = today's
  open tasks while notifications are on.
- Tests: unit (allowlist, body validation, event planning, texts, crypto,
  settings state) and e2e push.spec.ts against a local fake push service.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Otherwise a device subscribing later got the stale event on the next timer change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@antonsxh
antonsxh merged commit 53bd194 into main Nov 22, 2026
6 checks passed
@antonsxh
antonsxh deleted the mobile-push branch November 22, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant