flutter_local_notifications not showing Foreground notifications in release mode in devices above Android 6

2.4k Views Asked by At

I'm using Firebase Cloud Messaging(FCM) to send push notifications to my Flutter app. I need to show notifications in Foreground in Android, so tried the plugin -flutter_local_notifications as mentioned in the FlutterFire documentation, but it doesn't seem to work on all devices. I found it working only on one device running on Android 6.

I added this line in AndroidManifest.xml -

  <meta-data
            android:name="com.google.firebase.messaging.high_importance_channel"
            android:value="high_importance_channel"/>

How I'm triggering local notifications on receiving FCM message -

 // local notif initialisation //
    var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);

    /// Foreground
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (message.notification != null) {
        print('Foreground (onMessage): Title:${message.notification.title}, Subtitle:${message.notification.body}, Data:${message.data.toString()}');
        remoteMessage = message;
        var data = json.decode(message.data['metadata']);

        showNotification(
          1234,
          "${message.notification.title}",
          "${message.notification.body}",
          "$message",
        );
      }
    });

I have also tried creating custom notification channel like this

  AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    description: 'This channel is used for important notifications.', // description
    importance: Importance.max,
  );
 await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

I'm not sure if I'm missing something? Are there any additional steps for higher Android API levels. There was a note on the plugin's Readme titled Release build configuration calling for ProGuard file customisation, I followed those steps too, but nothing helped in my case. Looking for some help on this issue.

Dependency versions:

  firebase_core: ^1.13.1 
  firebase_messaging: ^11.2.8
  flutter_local_notifications: ^9.2.0

Flutter SDK Version : Flutter 2.10.2

1

There are 1 best solutions below

4
On

Add the proguard-rules.pro in android\app\ directory. As flutter_location_notification suggests which is

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

##---------------End: proguard configuration for Gson  ----------

You can try adding the following line in android\app\build.gradle > buildTypes > release

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

The buildTypes will look like following:

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
            signingConfig signingConfigs.release
        }