Pattern: Push notifications
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Values are examples.
- Core ideas: the server sends a notification type and variables, the phone writes the text in the person’s language; the lock screen can hide the title; the app notices a disabled permission; dead tokens are deleted when sending fails.
- Related: multi-language, rate limits, sessions.
1. Problem
Ready-made sentences sent from the server fix the language, expose private text on the lock screen, and fail silently when the user turns permission off or the device token dies.
2. Options
| Topic | Option | Upside | Downside |
|---|---|---|---|
| Text | Server sends finished text | Simple | One language per send; text is stored and logged server-side |
| Text | Server sends type and variables; the app builds the text (recommended) | Right language from the device; less private text in transit | App must handle every type and have strings for each language |
| Delivery | Via a middleman service | Quick to start | Extra party sees metadata; extra dependency |
| Delivery | Directly via the platform services (FCM, APNs) | Fewer parties; fewer moving parts | Two integrations to maintain |
3. Recommended design
- Type plus variables. Send
type: "new_comment"and variables such as the commenter’s display name, not a finished sentence. The device assembles the sentence from localised strings (ICU message format handles plurals; see multi-language). - Lock-screen privacy: offer a setting to hide the title on the lock screen (default off, with a short explanation under it). Practitioner experience: users in shared environments value it.
- Permission handling: Android 13 (API 33) and higher has a runtime permission for non-exempt notifications (
POST_NOTIFICATIONS); iOS asks the user to allow notifications (Apple’s “Asking permission to use notifications”). Ask at a moment when the benefit is clear, not at first launch. When permission is off, the app tells the user once and offers a button that opens system settings. - Dead tokens: FCM’s guidance on registration management warns that sending to inactive or stale registrations wastes resources and distorts delivery statistics; remove tokens when the service reports them invalid, and refresh tokens regularly. Delete dead tokens at send time.
- Direct services when possible: FCM for Android and APNs for iOS (“Setting up a remote notification server”), unless an intermediary gives you a clear benefit you need.
- Privacy: put as little personal data as possible in the payload; the visible text is shaped by the device settings.
- Rate-limit notification sends per user so a bug or abuse cannot flood a phone (see rate limits).
- Sign-out: remove the device token from the account on sign-out so the next user of the device does not receive the previous user’s notifications (sessions).
4. Scenarios and edge cases
- User changes language: future notifications follow the new device language automatically.
- Shared tablet: sign-out deletes the token on the server.
- Permission off: the in-app inbox still shows the item; a banner explains how to enable notifications.
- New notification type in a new app version: old app versions must ignore unknown types safely.
5. Common mistakes
- Sending finished text in one language.
- Private content on the lock screen by default.
- Asking for permission at first launch with no context.
- Never removing dead tokens.
- Tokens staying linked to an account after sign-out.
- Unbounded send loops.
6. How to audit
- What: payload contains a type and variables.
- How to check: capture a test push payload.
- Evidence: the payload.
- What: localisation follows device language.
- How to check: change the device language and trigger a notification.
- Evidence: screenshots in two languages.
- What: dead tokens are cleaned.
- How to check: send to an uninstalled device; check the token record afterwards.
- Evidence: the token deleted.
- What: sign-out removes the token.
- How to check: sign out, then trigger a notification for that account.
- Evidence: no delivery.
Sources
- Firebase Cloud Messaging — Firebase (Google)
- Best practices for FCM registration management — Firebase (Google)
- Setting up a remote notification server — Apple Developer Documentation
- Notification runtime permission — Android Developers
- Asking permission to use notifications — Apple Developer Documentation
- User Privacy Protection Cheat Sheet — OWASP Cheat Sheet Series
- ICU: Formatting Messages — Unicode ICU