Sub topic 1
How a Push Notification Travels from Server to Device
Prerequisites
This chapter assumes you know basic Flutter app structure, Dart async and await, streams, and the difference between Android and iOS application states. You should also have a Firebase project and a Flutter app that can run on a physical device or emulator.
A push notification is a message delivered to an app through an operating system service. In Flutter, Firebase Cloud Messaging (FCM) provides the common messaging layer. Your server sends a message to FCM. FCM then routes it through Android’s delivery system or Apple Push Notification service (APNs) before it reaches the device.
The delivery path
The complete path has several separate responsibilities:
- The Flutter app registers with FCM.
- FCM returns a registration token for that app installation.
- Your server stores the token.
- Your server sends a message to FCM.
- FCM routes the message to Android or APNs.
- The operating system delivers or displays the message.
- Flutter receives the message when the app state allows it.
The registration token identifies a particular app installation, not a user permanently. It can change after reinstalling the app, clearing app data, restoring a backup, or other platform events. Your app should report new tokens to your server.
Your server should send through the FCM HTTP v1 API or another trusted server-side integration. Do not place server credentials in the Flutter application. Anyone who extracts credentials from a client app could send messages as your project.
What FCM sends
FCM supports two important message parts:
| Message part | Main purpose | Typical background behavior |
|---|---|---|
| Notification payload | Ask the operating system to display text | The system often displays it automatically |
| Data payload | Deliver application-specific key-value data | The app handles it when platform rules allow |
A message can contain both parts. For example, a notification payload might display “Order shipped,” while a data payload contains route: /orders/42. Data values in FCM requests should be strings.
The app state changes how Flutter sees a message:
- In the foreground, your code receives the message through
FirebaseMessaging.onMessage. The operating system usually does not show a notification automatically. - In the background, a notification payload is commonly displayed by the operating system. A data-only message may invoke a background handler, subject to platform restrictions.
- In the terminated state, the operating system may display a notification. If the user taps it, Flutter can read the opening message when the app starts.
Delivery is not execution
FCM confirms that it accepted a send request. That does not guarantee that a user saw the notification or that your Dart code ran immediately. Doze mode, battery policies, network loss, permission settings, expired tokens, and user interaction can affect delivery.
Exercise
A server sends a message containing only a data payload while the app is in the foreground. Which component receives it first, and what should the app do?
Show answer
FCM routes it to the device, and Flutter receives it through FirebaseMessaging.onMessage. The app should inspect the data and decide whether to update its UI, refresh server data, or show a local notification.