Sub topic 3
Configuring Android and iOS (including APNs)
This chapter covers the native project changes that must be in place before firebase_messaging can deliver anything. It assumes you can build and run your Flutter app on a device or emulator. It also assumes you have a Firebase project and have already connected the app with firebase_core and the FlutterFire CLI, as in the previous chapter. You should know your way around AndroidManifest.xml and Xcode's "Signing & Capabilities" tab.
Most "notifications don't work" reports come from this layer rather than from Dart code. The Dart code can be correct while the native side silently drops every message.
What the native layer has to provide
FCM does not talk to an iOS device directly. For iOS, FCM hands the message to Apple Push Notification service (APNs), and APNs delivers it. So iOS needs an extra trust relationship between Apple and Firebase. On Android, Google Play services receives the message, so setup is mostly manifest configuration.
Android setup
Confirm the Firebase configuration files
When you ran flutterfire configure, it registered your Android app and wrote android/app/google-services.json. It also generated lib/firebase_options.dart. Confirm that the file exists and that its package_name matches the applicationId in android/app/build.gradle (or build.gradle.kts). A mismatch is a common cause of a token that is generated but never receives messages.
The Google Services Gradle plugin reads google-services.json at build time. Current Flutter templates and the FlutterFire CLI wire this plugin in for you. If you upgraded an old project, check that com.google.gms.google-services is applied in the app-level Gradle file.
Declare the notification permission
Android 13 (API 33) made notifications a runtime permission. An app that targets API 33 or higher must declare POST_NOTIFICATIONS in the manifest. Without the declaration, the system never shows a permission prompt, and notifications are blocked.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application>
<!-- existing application configuration -->
</application>
</manifest>Declaring the permission only makes the prompt possible. You still trigger the prompt from Dart with requestPermission(), which the next chapter covers. On Android 12 and lower the call succeeds without showing a dialog.
Set a default channel and icon (optional but recommended)
Since Android 8, every notification belongs to a notification channel. When FCM shows a notification for you while the app is in the background, it uses a default channel. You can name your own default channel and icon in the manifest so that notifications look consistent.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
</application>
</manifest>The channel id you name here must exist before a message arrives. If it does not, Android falls back to the Firebase default channel. You create the channel in code with a local notifications plugin, which a later chapter covers. The icon should be a white-on-transparent drawable. A full-color icon is rendered as a solid square on many devices.
Android gotchas
- Emulators need Google Play services. Use an emulator image labeled "Google Play" or "Google APIs". Plain AOSP images have no FCM client.
- Check the minimum SDK. The
firebase_messagingversion you use sets a minimumminSdk. The build fails with a manifest merger error if your value is lower, so read the plugin's changelog when you upgrade. - Force-stopped apps get nothing. If the user force-stops the app from system settings, Android blocks FCM delivery until the user launches the app again.
iOS setup
iOS has more steps, and each one fails silently if you skip it.
Requirements
You need a paid Apple Developer Program membership to create push credentials. You also need a physical iPhone or iPad for reliable end-to-end testing. Simulators on recent macOS and Xcode versions can show pushes you inject by hand, but they do not receive real messages through FCM the way a device does.
Add the capabilities in Xcode
Open ios/Runner.xcworkspace (not .xcodeproj) in Xcode, select the Runner target, and open Signing & Capabilities. Then do the following.
- Select a Team so the app is signed with your developer account.
- Click + Capability and add Push Notifications. This adds the
aps-environmententitlement. - Click + Capability again and add Background Modes. Check Remote notifications.
The Background Modes step writes the following key to ios/Runner/Info.plist. Without it, data-only messages never wake the app in the background.
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>Create and upload an APNs authentication key
Firebase needs credentials to send to APNs on your behalf. Apple offers two kinds: an authentication key (.p8 file) and certificates (.p12). Prefer the authentication key. One key works for all your apps and both the development and production environments, and it does not expire yearly like a certificate.
- In the Apple Developer account, open Certificates, Identifiers & Profiles, then Keys, and create a key with Apple Push Notifications service (APNs) enabled.
- Download the
.p8file. Apple lets you download it only once, so store it safely. Note the Key ID and your Team ID. - In the Firebase console, open Project settings, then Cloud Messaging, and find your iOS app. Upload the
.p8file and enter the Key ID and Team ID.
Make sure the app's Bundle Identifier in Xcode matches the one registered in Firebase. It must also have Push Notifications enabled on its App ID in your Apple account.
Method swizzling and the APNs token
By default, the Firebase SDK uses method swizzling. It intercepts the iOS callbacks that deliver the APNs device token and links that token to the FCM registration token for you. No AppDelegate code is needed.
If you set FirebaseAppDelegateProxyEnabled to NO in Info.plist, swizzling is off. You then must forward the APNs token to Firebase yourself in the AppDelegate. Leave swizzling on unless another plugin conflicts with it.
Verify the APNs token from Dart
On iOS, FCM can only produce a registration token after iOS has handed the app an APNs token. If you call getToken() too early, it can throw an apns-token-not-set error. This helper waits briefly for the APNs token and is useful when debugging.
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
Future<String?> waitForApnsToken() async {
if (!Platform.isIOS) return null;
final messaging = FirebaseMessaging.instance;
for (var attempt = 0; attempt < 10; attempt++) {
final apnsToken = await messaging.getAPNSToken();
if (apnsToken != null) {
debugPrint('APNs token: $apnsToken');
return apnsToken;
}
await Future<void>.delayed(const Duration(seconds: 1));
}
debugPrint('No APNs token. Check capabilities, signing, and device.');
return null;
}A permanently null result usually means one of four things:
- The Push Notifications capability is missing.
- The app is running on a simulator.
- The provisioning profile does not include push.
- The user denied permission.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Android 13 never shows a permission prompt | `POST_NOTIFICATIONS` not declared | Add the manifest permission and rebuild |
| Android notification icon is a gray square | Icon is full color | Use a white-on-transparent drawable |
| iOS token is null or throws `apns-token-not-set` | Missing capability, simulator, or denied permission | Add Push Notifications, use a device, request permission |
| Console sends succeed, iOS shows nothing | APNs key not uploaded or wrong Team ID or Key ID | Re-upload the `.p8` and recheck the IDs |
| iOS data messages never wake the app | Remote notifications background mode missing | Enable it in Background Modes |
Exercise
On iOS you added the Push Notifications capability and uploaded a valid .p8 key, but data-only messages never reach your app while it is in the background. Notification messages with a title and body do appear. Which setup step is the likely cause, and why does it affect only one message type?
Show answer
The Background Modes capability with "Remote notifications" (the UIBackgroundModes remote-notification key) is probably missing. A notification message is displayed by the operating system without running app code, so it does not need the background mode. A data-only message must wake the app to run Dart code, and iOS only permits that when the remote-notification background mode is enabled.
Exercise
Your Android 13 app targets API 34 and the Dart code calls requestPermission(), but no dialog appears and notifications are blocked. What do you check first?
Show answer
Check that android.permission.POST_NOTIFICATIONS is declared in AndroidManifest.xml. For apps targeting API 33 or higher, the system only shows the runtime prompt when the manifest declares the permission.