Sub topic 16
Why notifications sometimes do not arrive
This chapter assumes you can build and run a Flutter app, have firebase_messaging set up on Android and iOS, and know the difference between notification messages and data messages. It gives you a checklist for diagnosing a message that never shows up, and explains the platform limits behind the most common causes.
Delivery is best effort
FCM does not guarantee that a message reaches a device, or that it arrives quickly. A successful send response means FCM accepted the request. It does not mean the device received anything. Between FCM and the screen there are several places where a message can stop.
Work through the stages in order. Most problems sit at the first two or the last two.
Stage 1: the send request
Check these before touching app code.
- Invalid or stale token. If a token belongs to an uninstalled app or an expired registration, the HTTP v1 API returns
UNREGISTERED(HTTP 404). Delete that token from your server. - Wrong project. A token only works with the Firebase project that issued it. A debug build using a different
google-services.jsonorGoogleService-Info.plistthan your server's service account producesSENDER_ID_MISMATCH. - Malformed payload. Every value in
datamust be a string. A number or nested object causes anINVALID_ARGUMENTerror. Reserved keys such asfrom,message_type, or keys starting withgoogle.orgcm.cannot be used indata. - Payload too large. The limit is 4096 bytes for most messages (topic messages are also limited to 4096 bytes).
- Expired credentials. The HTTP v1 API needs a short-lived OAuth 2.0 access token. A token older than about an hour returns
UNAUTHENTICATED.
Always log the full response body of the send call. The error code usually names the cause.
Stage 2: FCM to the platform service
Time to live and offline devices
If the device is offline, FCM stores the message and delivers it when the device reconnects. It keeps the message for a time to live (TTL). The default is four weeks. If you set a short TTL, the message is dropped when it expires. A TTL of 0 on Android means "send now or never".
{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"data": { "route": "/orders/42" },
"android": {
"priority": "high",
"ttl": "3600s"
}
}
}Collapsing
A collapse_key (Android) or apns-collapse-id (iOS) tells FCM that a newer message replaces an older pending one. If a device was offline and you sent five messages with the same key, only the latest arrives. This is useful for "sync now" signals and surprising for chat messages.
Priority
Android messages have normal or high priority. Normal priority messages can be delayed while the device is in Doze mode. High priority messages wake the device, but FCM may lower the priority of an app that repeatedly sends high priority data messages that do not produce a user-visible notification. On iOS, the apns-priority header plays the same role: 10 is immediate, 5 is power-conscious, and 5 is required for background updates.
Stage 3: iOS-specific causes
iOS has the most setup steps, so it has the most silent failures.
- No APNs authentication key. Upload an APNs key (
.p8) in the Firebase console under Project settings, Cloud Messaging. Without it, FCM accepts your request and APNs never sees it. - Missing capabilities. In Xcode, enable Push Notifications and Background Modes with Remote notifications.
- Simulator limits. Simulators on Apple silicon can receive pushes since Xcode 14 on macOS 13, but older setups and many Intel configurations cannot. Test on a real device when results are unclear.
- Wrong environment. Debug builds use the APNs sandbox and release builds use production. A key-based setup handles both, but an old certificate-based setup does not.
- Background updates need
content-available. A data-only message on iOS must setapns.payload.aps.content-availableto1to wake the app. Even then, iOS throttles these and does not run them if the user force-quit the app.
Stage 4: Android-specific causes
- Missing
POST_NOTIFICATIONSpermission. On Android 13 and above, the app must declare it inAndroidManifest.xmland the user must grant it at runtime.requestPermissionhandles the prompt. - Muted notification channel. If the user turns off a channel, FCM notifications are still delivered to the device but the system does not display them.
- Force stop. After a user force-stops an app from system settings, Android blocks all broadcasts to it, including FCM, until the user opens the app again. This is deliberate, and you cannot work around it.
- Manufacturer battery managers. Some vendors aggressively kill background apps. Messages then arrive late or only when the user opens the app. The practical response is to use high priority for time-sensitive notifications and, where it matters, point users to their battery settings.
- Google Play services missing. Devices and emulator images without Play services cannot receive FCM.
Stage 5: the app itself
Foreground messages are not displayed
When the app is visible, FCM delivers a notification message to onMessage and the system does not draw a banner. This is correct behavior, not a bug. To show something, use a local notifications plugin, or on iOS call setForegroundNotificationPresentationOptions:
import 'package:firebase_messaging/firebase_messaging.dart';
Future<void> showForegroundBannersOnIos() async {
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}Background handler mistakes
A background handler that never runs usually has one of these causes:
- It is not a top-level or static function.
- It lacks
@pragma('vm:entry-point'), so release builds strip it through tree shaking. - It is registered after something that throws, or registered inside a widget instead of
main. - It tries to touch UI or plugins that need the main isolate. It runs in a separate isolate, so it must call
Firebase.initializeAppitself and cannot read state from your widgets.
Handler shows nothing for a notification message
A message with a notification block is displayed by the system when the app is in the background or terminated. Your handler may still run on Android, but it is not what draws the notification. If you want to control the display, send a data-only message and build the notification yourself with a local notifications plugin.
Taps do nothing
Check that you cover all three entry points: onMessage for the foreground, onMessageOpenedApp for background taps, and getInitialMessage for launches from the terminated state. Call getInitialMessage once, after your navigator is ready, otherwise the navigation is lost.
Quick diagnosis table
| Symptom | Likely cause | First thing to check |
|---|---|---|
| Send call returns an error | Bad token, payload, or credentials | Error code in the response body |
| Send succeeds, nothing on iOS | APNs key missing or capability not enabled | Firebase console APNs settings, Xcode capabilities |
| Works in debug, fails in release | Handler stripped or wrong environment | `@pragma('vm:entry-point')`, APNs key type |
| Nothing on Android 13+ | Permission not granted | `POST_NOTIFICATIONS` in manifest and runtime result |
| Works until the app is force-stopped | OS force-stop block | Reopen the app once and retest |
| Arrives late | Normal priority or Doze | Set high priority, test with the device awake |
| Foreground shows nothing | Expected behavior | Local notification or iOS presentation options |
| Only the latest of several messages arrives | Collapse key | Remove or vary `collapse_key` |
A systematic debugging routine
- Print the token with
getToken()and confirm it is the one your server uses. - Send from the Firebase console to that single token. If this works and your server does not, the problem is in your request.
- Test in each app state: foreground, background, terminated (swipe away, not force-stop from settings).
- Check the permission status with
getNotificationSettings()and the system notification settings for the app. - On Android, run
adb logcatand filter forFirebaseMessagingandGCM. On iOS, read the Xcode device console. - Try the Firebase console's delivery data under Reports to see sent and received counts per message.
- Retest on a second physical device to separate device-specific battery and vendor issues from your own code.
Exercise
Your server sends a data-only message to an iPhone. The Firebase API returns success, the app is running in the background, but the background handler never runs. List three possible causes.
Show answer
Any three of the following. The message lacks apns.payload.aps.content-available: 1, so iOS does not wake the app. The user force-quit the app, and iOS does not launch it for background updates. iOS throttled the delivery because of low-priority or power-saving conditions, or the apns-priority is wrong. The Remote notifications background mode is not enabled in Xcode. The APNs key is missing from the Firebase project. The handler is not top-level or lacks @pragma('vm:entry-point').
Exercise
A user reports that notifications stopped arriving on their Android phone after they swiped the app away from the recents list, but they worked before. Another user force-stopped the app from system settings. Which case is the OS blocking FCM, and what does it take to fix it?
Show answer
Force-stopping from system settings is the blocking case. Android does not deliver FCM broadcasts to a force-stopped app until the user opens it again. Swiping from recents does not force-stop the app, so that user's problem lies elsewhere, such as battery restrictions, a muted channel, or revoked permission. The force-stopped user fixes it by opening the app once.