Home > Mobile >  How keep AndroidNotificationDetails in background FCM/Flutter
How keep AndroidNotificationDetails in background FCM/Flutter

Time:09-06

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 ?

CodePudding user response:

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>
  • Related