Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How keep AndroidNotificationDetails in background FCM/Flutter

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading