ylliX - Online Advertising Network How to Receive Push Notifications in Flutter When the App is Killed - Yet Another Programmer's Blog

How to Receive Push Notifications in Flutter When the App is Killed

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 added
  • google-services.json placed in your android/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 and FirebaseMessaging.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 notificationblock, 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

  1. Missing notification in payload: System notifications won’t be shown when the app is killed.
  2. Low priority messages: Always use "priority": "high".
  3. No notification permission (Android 13+): Use FirebaseMessaging.requestPermission() and prompt the user.
  4. Battery optimizations: Some OEMs like Xiaomi or Samsung may block background services.

🔄 Testing

  1. Use the Firebase Console to send a test push with a notification payload.
  2. Manually kill the app from the task switcher.
  3. Lock the device or keep it idle.
  4. 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!

 

 

Leave a Reply