Flutter Push Notifications With Firebase Cloud Messaging

· 6 min read

Firebase Cloud Messaging (FCM) is the default way most Flutter apps send push notifications. Firebase describes it as “a cross-platform messaging solution that lets you reliably send messages.” One SDK covers Android, iOS and web, and your server talks to one API instead of two.

This guide walks through setup, the newer registration flow, message handling in each app state, and sending a message from your backend. Every step comes from the current FlutterFire and Firebase docs.

Before you start

You need a Flutter app already connected to a Firebase project. If you have not done that, start with Firebase in a Flutter app with Claude Code. Then add the plugin:

flutter pub add firebase_messaging

Two platform facts to know up front:

  • Android: “FCM clients require devices running Android 7.0 or higher that also have Google Play services installed.”
  • iOS: FCM delivers through Apple Push Notification service (APNs), so you still need Apple-side setup. FCM does not replace it.

iOS setup

The FlutterFire client docs list these steps:

  1. In Xcode, enable Push Notifications under Signing & Capabilities.
  2. Add Background Modes and enable both “Background fetch” and “Remote notifications.”
  3. Create an APNs authentication key (a .p8 file) in your Apple developer account.
  4. Upload that key in the Firebase console under Project Settings, then Cloud Messaging, with its key ID and your team ID.

The docs also note that the plugin relies on method swizzling, which must stay enabled. The Simulator only takes simulated pushes sent with simctl, so test real FCM delivery on a physical iPhone. If you want a quick local check of your handler code, see our post on the iOS Simulator from Claude Code.

Ask for permission

On iOS, and on Android 13 and higher, the user must allow notifications. The plugin call is:

final settings = await FirebaseMessaging.instance.requestPermission(
  alert: true,
  badge: true,
  sound: true,
  provisional: false,
);

On Android, Google explains that apps targeting Android 13 need the POST_NOTIFICATIONS permission and that on new installs “notifications are off by default.” Add it to your manifest:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

Timing matters more than code here. Google recommends asking in context, after the user does something where notifications clearly help. Our push notification best practices post covers when and how to ask.

Register the device: the new flow

This is the part most older tutorials get wrong now. The FlutterFire docs mark getToken() and onTokenRefresh with “This feature is deprecated” and say “this method will be removed in a future release.”

The replacement uses the Firebase Installation ID (FID). You listen for registration, then register:

final messaging = FirebaseMessaging.instance;

messaging.onRegistered.listen((String installationId) {
  // Send this ID to your server and store it with the user.
  api.saveInstallationId(installationId);
});

await messaging.register(vapidKey: 'YOUR_WEB_VAPID_KEY');

The docs say register() is “Required on Web, and on Android/Apple if auto-initialization is disabled.” They also warn: “Don’t use both the Firebase Installation ID APIs and the deprecated registration token APIs for registration management at the same time.” Pick one approach and stick with it.

On Apple platforms, the docs add one more rule: “make sure the APNS token is available before making any FCM plugin API calls.” Calling getAPNSToken() first and checking it is not null avoids a common startup error.

If your existing app still uses getToken(), it keeps working for now, but plan the migration before the removal lands.

Handle messages in each app state

Firebase defines three states:

  • Foreground: “When the application is open, in view and in use.”
  • Background: “When the application is open, but in the background (minimized).”
  • Terminated: “When the device is locked or the application is not running.”

Each state has its own handler.

Foreground

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  // Update UI, show an in-app banner, refresh data.
});

Foreground notifications are not displayed by default. The docs say: “On Android, you must create a ‘High Priority’ notification channel.” and “On iOS, you can update the presentation options for the application.” Many apps show their own in-app banner instead, or use local notifications to display one.

Background and terminated

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  // Light work only: save data, update a counter.
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(const MyApp());
}

The rules from the docs:

  • The handler must not be an anonymous function.
  • It must be a top-level function, not a class method.
  • On Flutter 3.3.0 and later, annotate it with @pragma('vm:entry-point') so it is not removed in release builds.
  • On Android it runs in its own isolate, so it cannot update your app’s UI directly.

Notification taps

When the user taps a notification, you usually want to open a screen. Two APIs cover this:

  • getInitialMessage(): “If the application is opened from a terminated state a Future containing a RemoteMessage will be returned.”
  • onMessageOpenedApp: a stream for taps while the app is in the background.
final initial = await FirebaseMessaging.instance.getInitialMessage();
if (initial != null) _openFromMessage(initial);

FirebaseMessaging.onMessageOpenedApp.listen(_openFromMessage);

Route inside _openFromMessage with your router. Our guide on deep links with go_router shows how to map a payload field to a route.

Notification vs data messages

FCM has two message types. Notification messages carry a title and body that the system can display for you. Data messages carry only your own key-value pairs, and your app decides what to do. You can combine both.

Size is limited. Firebase states “a message can transfer a payload of up to 4096 bytes to a client app.” Send an ID, not a full record, and let the app fetch the rest.

Send a message from your server

The current server API is the HTTP v1 API. You authenticate with a short-lived OAuth 2.0 access token from a service account, using the scope https://www.googleapis.com/auth/firebase.messaging.

A raw request looks like this:

curl -X POST \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send \
  -d '{
    "message": {
      "token": "DEVICE_REGISTRATION_TOKEN",
      "notification": {"title": "Order shipped", "body": "Your order is on its way"},
      "data": {"orderId": "123"}
    }
  }'

You can target a single device, a topic, or a condition that combines topics. Firebase lists the older device group field as “deprecated, will be removed on September 29, 2027”, so do not build new features on it.

Never put a service account key in your Flutter app. Sending always happens from a trusted server.

Common mistakes

  • Testing iOS before uploading the APNs key. Nothing arrives and no error shows in the app.
  • A background handler inside a class. It must be top-level.
  • Asking for permission on first launch. Ask after a relevant action.
  • Mixing old and new registration APIs. The docs say not to.
  • Storing only one token per user. Users have several devices. Store a list.
  • No tap handling. A notification that opens the home screen feels broken.

Want to skip Firebase?

If you only need iOS or have a privacy-focused audience, read Flutter push notifications without Firebase. It covers direct APNs and UnifiedPush.

For the wider build flow, including how to get an agent to wire this up from a written spec, see the Flutter with Claude Code guide. MobileKit’s retention skill includes notification rules like channel setup and deep links, which saves writing them yourself.

Sources read on the day this post was written: Firebase: Set up a Firebase Cloud Messaging client app on Flutter, Firebase: Receive messages in a Flutter app, Firebase Cloud Messaging overview, Firebase: Send a message using the HTTP v1 API and Android Developers: Notification runtime permission.

Questions

Do I still use getToken() for FCM in Flutter?

The FlutterFire docs now mark getToken() and onTokenRefresh as deprecated and say they will be removed in a future release. The recommended flow is to listen to onRegistered for the Firebase Installation ID and send that ID to your server.

Why don't my FCM notifications show while the app is open?

Foreground notifications are not shown by default. On Android you must create a high priority notification channel, and on iOS you update the presentation options for the app. Data still arrives in the onMessage stream.

Does FCM work on Android phones without Google Play services?

No. Firebase says FCM clients require devices running Android 7.0 or higher that also have Google Play services installed.

Try it on your next idea

One payment, every update to this major version, and a refund window if it is not for you. Refund policy.