Push notifications are table stakes for any serious mobile app — and Firebase Cloud Messaging (FCM) remains the default choice for Flutter. But between Android 13+ permission changes, iOS APNs quirks, and the three app states (foreground, background, terminated), getting them right takes more than the 5-minute quickstart suggests. This is the complete guide I wish I had.
1. Setup: Packages and Firebase Project
Add the packages:
\ yaml
dependencies:
firebase_core: ^3.8.0
firebase_messaging: ^15.2.0
\ \
Initialize Firebase before runApp\ , and register the background handler before anything else:
\ `dart
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
// MUST be a top-level function — not a class method
@pragma ('vm:entry-point')
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
debugPrint('Background message: \${message.notification?.title}');
}
Future main() async…