Sub topic 3
Configuring Android and iOS (including APNs)
Assume you know basic Flutter project structure, async and await, and how to run an app on a physical Android or iOS device. You also need a Firebase project and an app whose Android application ID and iOS bundle identifier are known.
Add Firebase to the Flutter project
From the project directory, add the Firebase packages and run the FlutterFire CLI:
flutter pub add firebase_core
flutter pub add firebase_messaging
dart pub global activate flutterfire_cli
flutterfire configureChoose the Firebase project and the Android and iOS platforms. The CLI creates firebase_options.dart and downloads the platform configuration files. Keep the generated file in the project. Do not commit private server credentials or service-account keys.
Initialize Firebase before registering messaging handlers:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/widgets.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const Placeholder());
}The Android application ID and iOS bundle identifier must match the apps registered in Firebase. A mismatch can produce valid-looking builds that cannot receive messages.
Configure Android
The FlutterFire configuration normally applies the Google services Gradle setup. Verify that the generated Android configuration belongs to the same Firebase project as your app.
Android 13 and later require the notification permission before the app can display notifications. Declare it in android/app/src/main/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:label="notification_demo"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Existing configuration -->
</application>
</manifest>The declaration alone is not enough. The app must request the permission at runtime. Handle that request in the permission chapter, and test it on an Android 13 or newer device.
Configure iOS and APNs
Firebase Cloud Messaging uses Apple Push Notification service, or APNs, to deliver messages to iOS devices. In Xcode, open ios/Runner.xcworkspace, select the Runner target, and add these capabilities:
- Push Notifications
- Background Modes, with Remote notifications enabled
In the Firebase console, open Project settings, select Cloud Messaging, and configure the Apple app with an APNs authentication key. Upload the .p8 key and provide its Key ID and Apple Team ID. The iOS bundle identifier in Firebase must exactly match the identifier in Xcode.
Use a physical iOS device for delivery tests. The iOS simulator does not provide the same APNs behavior as a physical device.
flowchart LR
A[Flutter app] --> B[Firebase configuration]
B --> C[FCM]
C --> D[Android device]
C --> E[APNs]
E --> F[iOS device]caption: Platform-specific paths used by Firebase Cloud Messaging alt: A Flutter app connects to Firebase Cloud Messaging, which sends directly to Android or through APNs to iOS.
Common setup failures
| Symptom | Likely cause |
|---|---|
| Android app builds but notifications fail | Wrong Firebase project or application ID |
| iOS token is missing | APNs capability, Firebase APNs credentials, or permission setup is incomplete |
| iOS works in one build but not another | Development and production signing environments differ |
| Notifications appear nowhere on Android 13+ | `POST_NOTIFICATIONS` was not granted |
Exercise
Why can an iOS Flutter app initialize Firebase successfully but still fail to receive push notifications?
Show answer
Firebase initialization does not configure APNs. The app also needs the correct bundle identifier, Push Notifications capability, valid APNs credentials in Firebase, and permission from the user.