# Phone features and notifications (lesson 7)

## What a web app can do on a phone
Checked November 2026: iPhone from https://firt.dev/notes/pwa-ios/, Android from Chrome's docs. This changes with each iOS release: check before promising a feature, and say plainly when something won't work on one platform.

| Feature | iPhone | Android |
|---|---|---|
| Notifications (web push) | Yes, once on the home screen (iOS 16.4+) | Yes |
| Badge on the icon (`navigator.setAppBadge`) | Yes, once on the home screen | No API; shows a dot while a notification is unread |
| Works offline (service worker) | Yes | Yes |
| Keep the screen on (Wake Lock) | Yes (home-screen apps from iOS 18.4) | Yes |
| Share out (`navigator.share`) | Yes | Yes |
| Share in (manifest `share_target`) | No | Yes, installed |
| Location (while open) | Yes | Yes |
| Camera and microphone | Yes | Yes |
| Passkeys / Face ID / fingerprint (WebAuthn) | Yes | Yes |
| Apple Pay / Google Pay (Payment Request) | Yes | Yes |
| Lock-screen media controls (Media Session) | Yes | Yes |
| Speech (synthesis and recognition) | Yes | Yes |
| Motion and orientation | Yes (motion needs a permission tap) | Yes |
| Vibration | No | Yes |
| Background Sync | No | Yes |
| App shortcuts (manifest `shortcuts`) | No | Yes |
| Bluetooth, NFC | No | Yes |

Not possible for any web app: home-screen widgets, background location, running continuously in the background, being found in Apple's App Store. On Android it can go on Google Play wrapped as a Trusted Web Activity (PWABuilder); Google charges a one-off $25 developer fee.

Rules:
- Ask for any permission (notifications, location, camera, motion) only after the user taps something that needs it, with one line saying why. Never on page load.
- Feature-detect (`if ("setAppBadge" in navigator)`) and hide what isn't supported, rather than showing a button that fails.
- If iPhone needs the home-screen app for a feature, detect Safari (`display-mode: standalone` false and `navigator.standalone` not true) and say "Open it from your Home Screen to turn this on" instead.

## Notifications (web push): lessons from a feeding log with reminders day and night
Setup:
- VAPID key pair, generated once, stored as Worker secrets (`VAPID_PUBLIC_KEY`, `VAPID_PRIVATE_JWK`, `VAPID_SUBJECT`). Never regenerate: a new pair means every phone must turn reminders on again.
- Push encryption (RFC 8291 aes128gcm) and VAPID signing (RFC 8292) work with Web Crypto alone in a Worker; no Node libraries needed. Unit-test the encryption.
- A Cron Trigger (e.g. every 5 minutes, free plan) works out what's due and sends it. Store each phone's time zone with its subscription and do time maths in that zone.
- A sent-log table (endpoint, kind, ref, sent_at) so each reminder goes once even if the cron runs again; prune it after a few days.

In the app:
- A clear "Turn on reminders" button in Settings. On iPhone, `Notification.requestPermission()` must be the first thing after the tap (no `await` before it), or the prompt never shows.
- Each phone chooses its own reminders and quiet hours. Show which phones have reminders on.
- If permission is "denied", explain how to change it in the phone's settings; the browser won't ask again.
- In the service worker, every `push` must show a notification (iPhone revokes subscriptions that push silently). Use `tag` + `renotify` so a newer reminder replaces an older one instead of stacking. `notificationclick` focuses or opens the app.

On the server:
- Accept subscriptions only for real push services (`push.apple.com`, `fcm.googleapis.com`, `push.services.mozilla.com`, `notify.windows.com`), since the server will POST to whatever address it's given.
- On 404 or 410, delete the subscription (the phone unsubscribed or reinstalled). Count other failures and give up after many.
- Only people allowed to see the data get reminders about it; re-check roles at send time.
- Keep private content off the lock screen: "A new note", not the note.
- Delete push subscriptions when a user signs out of that phone or deletes their account.

Be honest:
- Delivery is best effort: reminders can arrive a few minutes late, and Focus/Sleep can silence them (web apps can't use iPhone Critical Alerts). Say so in Settings so nobody relies on them alone.

Testing:
- Locally: `npx wrangler dev --test-scheduled`, then trigger the cron with the local scheduled endpoint. A dev-only flag can allow a local fake push service; never set it on the live app.
- Then a real test on the user's own phone, from the home-screen app, before anyone else gets one.
