[FIXED] How keep AndroidNotificationDetails in background FCM/Flutter

Issue

How keep my AndroidNotificationDetails when the app is in background/app closed ?

Here the code of FirebaseMessaging.onMessage :

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;

      flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(
            channel.id,
            channel.name,
            channelDescription: channel.description,
            color: ControllerColors.getPrimaryColor(), // Color orange
            icon: "@mipmap/ic_launcher"
           )
         )
      );
    });

Here the code when I handle the background :

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage remoteMessage) async
{
  await Firebase.initializeApp();
  await setupFlutterNotification();
  FirebaseMessaging fcm = FirebaseMessaging.instance;
  await fcm.getToken();
}

All works very good but my app_name color (orange) in AndroidNotificationDetails is not orange when the app is in background.

How resolve it ?

Solution

Write these codes inside the application in the android/app/src/main/AndroidManifest.xml file:

   <application
        android:usesCleartextTraffic="true"
        android:label="your app name"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
       <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
           android:resource="@drawable/notification_icon" />
       <meta-data
           android:name="com.google.firebase.messaging.default_notification_color"
           android:resource="@color/colorPrimary" />
.....
   </application>

Then put notification icon .png (with name – notification_icon) in the android/app/src/main/res/drawable folder.
Then write this code in android/app/src/main/res/values/styles.xml ​​file:

<resources>
    <!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off -->
    <color name="colorPrimary">#your_hex_color</color>
....
</resources>

Answered By – Sulaymon Ne'matov

Answer Checked By – Senaida (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published