Sub topic 15
Testing and Debugging Notification Delivery
Assume that you already have a Flutter app connected to Firebase, firebase_messaging configured, and a physical Android or iOS device available. You should also understand Dart async/await, streams, and the three app states: foreground, background, and terminated.
Build a test matrix
A notification can fail at several points. Test one variable at a time and record the result.
| Test | App state | Message | Expected result |
|---|---|---|---|
| A | Foreground | Notification plus data | `onMessage` runs; no system banner is guaranteed |
| B | Background | Notification plus data | The system displays the notification; tapping it opens the app |
| C | Terminated | Notification plus data | Tapping launches the app; `getInitialMessage()` returns the message |
| D | Any state | Data-only message | Your handler receives data only when platform delivery rules allow it |
Use a real device for final testing. An iOS simulator does not provide normal remote APNs delivery. An Android emulator needs Google Play services and a working network connection.
Add temporary diagnostic logging
Log the token, message ID, data, and app-state entry point. Do not log credentials or private message contents in production.
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
void logMessage(String source, RemoteMessage message) {
debugPrint(
'$source id=${message.messageId} '
'title=${message.notification?.title} '
'data=${message.data}',
);
}
void listenForDiagnostics() {
FirebaseMessaging.onMessage.listen(
(message) => logMessage('foreground', message),
);
FirebaseMessaging.onMessageOpenedApp.listen(
(message) => logMessage('opened-from-background', message),
);
}
Future<void> logLaunchMessage() async {
final message = await FirebaseMessaging.instance.getInitialMessage();
if (message != null) {
logMessage('opened-from-terminated', message);
}
}Run the app from a terminal so you can see debugPrint output:
flutter runConfirm that the token belongs to the device currently being tested. Tokens can change after reinstalling the app, clearing app data, restoring a backup, or changing the device.
Check the delivery path
Start at the sender and move toward the device:
- Confirm that the Firebase project is the same project used by the app.
- Confirm that the target token is current and has no surrounding spaces.
- Send a simple notification from the Firebase console.
- Check whether the message appears in the foreground, background, and terminated tests.
- If a tap should open a screen, verify that the data contains the expected route key.
- Inspect Android Logcat or Xcode device logs for permission, token, and APNs errors.
A console notification proves basic delivery, but it may not test your production server's authentication or HTTP v1 request body. Test those separately with a service account or another approved server-side credential. Never place that credential in the Flutter app.
Common causes of failure
- Notifications are denied in system settings.
- Android 13 or newer lacks
POST_NOTIFICATIONS, or the app never requested permission. - iOS lacks notification permission, Push Notifications capability, or valid APNs configuration.
- The app is in the foreground, where FCM does not automatically show a banner.
- A data-only message is delayed or dropped by battery restrictions and platform background limits.
- The server sends invalid data values. FCM data values must be strings.
- The user force-stopped the Android app. Background delivery can remain unavailable until the user opens it again.
- The server stores a stale token. Remove tokens after permanent delivery errors and save refreshed tokens.
Exercise
A message appears while the app is backgrounded but not while it is open. Is delivery necessarily broken?
Show answer
No. Foreground messages call onMessage, but FCM does not guarantee a system banner in that state. Add a local-notifications implementation if the app needs a visible foreground alert.