DEV Community

Cover image for Notification Service, Part 5: Push with Firebase
Surhid Amatya
Surhid Amatya

Posted on

Notification Service, Part 5: Push with Firebase

Push notifications reach iOS and Android through Firebase Cloud Messaging (FCM). The service never talks to a phone directly. It hands the message to Firebase, and Firebase delivers it using each device's token. This post covers how that call is made and what happens to bad tokens.

Where tokens come from

Device tokens arrive inside the Kafka event. The owning service attaches the user's active tokens when it publishes. People have more than one device, so an event often carries a list.

An event with no tokens isn't an error. The listener records the delivery status as SKIPPED and moves on.

Multicast: one call, many devices

For a list of tokens the service uses Firebase's multicast send. One call carries the message to every token, and the response reports success or failure per token, in request order. The service uses that to count successes and failures for each token, so metrics count devices that got the message, whatever the number of calls. It also tells the service exactly which tokens failed.

Single-token and topic sends exist as well. The manual push endpoint uses them, for example.

Dead tokens

Tokens go stale when an app is uninstalled or reinstalled, and Firebase reports that per token. The service treats two cases as dead: the token is reported as unregistered, or it's rejected as invalid in the specific way Firebase reports a malformed or expired token.

For each dead token the service publishes an unregistered-fcm-token event to Kafka. The service that owns the user's device records consumes it and deactivates the token. The notification service never stores or edits tokens. It reports what it learned and leaves the data with its owner. Other failures, like a temporary Firebase problem, don't trigger any token removal.

A slow or failing provider

Firebase calls sit behind a circuit breaker. If at least half of the last 20 calls fail, or calls are consistently slower than 5 seconds, the breaker opens for 30 seconds. While it's open, calls fail immediately instead of waiting on a struggling provider. After the 30 seconds a few test calls go through, and the breaker closes again if they succeed.

When it's open, the service records the failure for the affected devices and logs it. It doesn't crash and it doesn't block other events.

What the listener records

After the attempt, the listener sets the stored notification's delivery status to SENT or FAILED. That update runs asynchronously on the capped executor from Part 7, so it doesn't slow down the main flow.

Where this leaves us

Push is a thin layer with a few guards: one multicast call per user, per-token results feeding the metrics, dead tokens reported by event for their owner to clean up, and a circuit breaker so a Firebase outage can't drag the service down. Failed pushes aren't retried automatically today.

Top comments (0)