One of the most common frustrations developers face when integrating push notifications in Flutter apps is ensuring that messages are delivered even when the app is terminated (“killed”). This guide explains how to configure your Flutter app using Firebase Cloud Messaging (FCM) so that notifications are reliably received even in this state.
🔧 Prerequisites
Before we dive in, make sure you have:
- A working Flutter app
- Firebase set up with your project
firebase_messaging
plugin addedgoogle-services.json
placed in yourandroid/app
directory
🌐 Ensure Proper FCM Setup
Add the Firebase Messaging dependencies in your android/app/build.gradle.kts
:
plugins { id("com.google.gms.google-services") id("com.google.firebase.crashlytics") id("dev.flutter.flutter-gradle-plugin") id("com.android.application") id("kotlin-android") } dependencies { implementation(platform("com.google.firebase:firebase-bom:32.7.0")) implementation("com.google.firebase:firebase-messaging") coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4") }
📄 AndroidManifest.xml Configuration
Make sure you include the necessary permissions in your android/app/src/main/AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
You do not need to declare FirebaseMessagingService
manually unless you’re creating a custom service.
🫠 FirebaseMessagingService Setup in Dart
You don’t need a native service. In your Dart service class, make sure you:
- Call
FirebaseMessaging.instance.getToken()
- Listen to
FirebaseMessaging.onMessage
andFirebaseMessaging.onMessageOpenedApp
- Use
flutter_local_notifications
to show custom foreground notifications
FirebaseMessaging.onMessage.listen((RemoteMessage message) { // Show notification using flutter_local_notifications }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { // Navigate to a specific screen });
🚀 Sending Notifications
To make sure your notification is shown when the app is killed, you must send a payload that includes a notification
block, not just data:
{ "message": { "token": "your-device-fcm-token", "notification": { "title": "Hello!", "body": "This will show even if the app is killed." }, "android": { "priority": "high" } } }
If you send only data
, Android won’t display the notification unless your app is running in the foreground or background.
⚠️ Common Pitfalls
- Missing
notification in payload: System notifications won’t be shown when the app is killed.
- Low priority messages: Always use
"priority": "high"
. - No notification permission (Android 13+): Use
FirebaseMessaging.requestPermission()
and prompt the user. - Battery optimizations: Some OEMs like Xiaomi or Samsung may block background services.
🔄 Testing
- Use the Firebase Console to send a test push with a
notification
payload. - Manually kill the app from the task switcher.
- Lock the device or keep it idle.
- Wait for the notification to appear.
🌟 Conclusion
Receiving FCM push notifications in a Flutter app when the app is killed is entirely possible with the right setup. Focus on sending the correct payload and ensuring permissions and dependencies are properly configured. Once it works, your app will be ready for production-grade messaging.
Need help debugging? Leave a comment or reach out!