FCM push notifications from a PHP backend to a Flutter app
fcmfirebasepushphpflutterdeep-linksnotificationsthrottling
TLDR sends push notifications for replies, mentions, follows, DMs, group messages, reminders and AI
status changes. The server is plain PHP with no Firebase SDK; the app uses firebase_messaging.
The recipe below has been reused for every new notification type.
Server side in brief
FCM HTTP v1 from PHP with no SDK (service-account JWT, OAuth token, messages:send) is described in
Server-sent push notifications (FCM HTTP v1, Web Push, ntfy). TLDR specifics:
- The service account JSON lives outside the web root and out of git.
- A device_tokens table (username, token UNIQUE, platform, created_at); the app registers on login
and on onTokenRefresh, unregisters on logout.
- sendToUser($user, $title, $body, $data = []): the optional data map carries machine-readable
fields (type, post_id, status, a route link). Adding it with a default kept every old call
site working.
Client
PushNotificationServicesingleton: request permission, get and register the token, handle foreground messages and taps. Store its stream subscriptions and cancel before re-listening, or re-initializing on each login duplicates every handler.- Deep links: on tap, stash the link (
_pendingLink) and let the app shell consume it both when resuming from background and ininitStatefor a cold start. - Foreground messages do not show a system notification by default: show an in-app snackbar with a "View" action instead, and skip it when the user is already on that conversation.
- Android:
POST_NOTIFICATIONSpermission and a default channel id in the manifest. - Data-only status pushes: AI status changes are sent with
{type: ai_status, post_id, status}; the handler updates the open screen and returns before touching the unread badge. Web has no FCM here, so web screens poll (only while something is pending; see Stop Flutter timers from polling while the app is in the background).
Rules that came from bugs
- Check visibility before every notification that links to content. Several paths notified
people about private posts they could not open. Use one
canView()helper; see API authorization from the verified identity (and the holes it closes). - E2E content never goes in a push. DMs say "New message from X"; group messages use the group name as the title and a generic body.
- Throttle chatty sources: at most 1 push per 30 s per conversation (or per sender per group);
use a collapse key like
dm_<conversationId>so the shade groups them. - Respect mute (per conversation or group) and never notify the sender.
- Do not fan out pushes synchronously inside a user-facing request. A scheduled-post publish used to
loop over followers sending FCM inside
GET /posts; it moved to a per-minute cron.
Reminders via cron
A reminder table holds reminder_at in America/Chicago wall-clock time, not UTC (that is how
the processing code compares it). A per-minute cron writes the notification row and sends a push
titled "Reminder" with the linked post's title as the body. Inserting a row directly is a valid way
to schedule one from a script.
See also
- E2E group chat with per-epoch sender keys
- Android notifications and PendingIntent pitfalls
- Server-sent push notifications (FCM HTTP v1, Web Push, ntfy)