Salar Azad
Salar Azad

Reputation: 300

Icon with Fluutter Background Service is Different

I'm using 'flutter_background_service' package with Flutter to run a service in the background, when setting up the I need to configure the Notification Channel.

The notification is now being shown but the title and description I set but the App icon is different than my App icon; how can fix this and make it show my real app icon?

Text

Upvotes: 1

Views: 1222

Answers (1)

Salar Azad
Salar Azad

Reputation: 300

For anyone who might be looking for the answer, this is what I did and it is working fine so far.

  1. As mentioned in the first comment, we should add the app icon image 'ic_bg_service_small.png' in the 'Android/app/src/main/res/@mipmap' folders which are 'mipmap-hdpi', 'mipmap-mdpi', 'mipmap-xhdpi', 'mipmap-xxhdpi' and 'mipmap-xxxhdpi'.

  2. add this entry to your AndroidManifest.xml file:

     <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
         android:resource="@mipmap/ic_bg_service_small"/>
    
  • 'ic_bg_service_small' name should be the same as the one in the '@mipmap' folders.
  1. Run the background service normally, which contains two parts.

first part is creating the local notification:

    await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(
        channel,
      );   

And the second part is configuring the background service:

    await bgServiceInstance.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
      notificationChannelId: BackgroundServiceClass.notificationChannelId,
      initialNotificationTitle: BackgroundServiceClass.notificationTitle,
      initialNotificationContent: BackgroundServiceClass.notificationContent,
      foregroundServiceNotificationId: BackgroundServiceClass.notificationId,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
  
  1. Inside the Background service (onStart), run a periodic timer with a function that keeps updating the notification badge icon:

     Timer backgroundServiceTimer =
       Timer.periodic(const Duration(seconds: 1), (timer) async {
     if (bgService is AndroidServiceInstance) {
       if (await bgService.isForegroundService()) {
         flutterLocalNotificationsPlugin.show(
           BackgroundServiceClass.notificationId,
           BackgroundServiceClass
               .notificationTitle,
           BackgroundServiceClass.notificationContent,
           const NotificationDetails(
             android: AndroidNotificationDetails(
               BackgroundServiceClass.notificationChannelId,
               BackgroundServiceClass
                   .notificationChannelName,
               icon: BackgroundServiceClass.notificationIcon,
               ongoing: true,
             ),
           ),
         );
       }
     }
    

    });

Point 4 is the most important part, which keeps updating the local notification content and its icon - without this function, then the local notification content will not be updated and its icon will be reverted to a default icon as shown in my original post.

that's all, now the correct notification icon should be shown always.

Upvotes: 0

Related Questions