Akila Hasintha
Akila Hasintha

Reputation: 7

How to Run a Foreground Service Without a Persistent Notification in Flutter?

I am working on a Flutter project where I need to run a background service that continuously sends location updates. I'm using the flutter_background_service package to achieve this. However, I want to avoid showing a persistent notification when the service is running in the foreground.

What I've Tried:

Running the Service in Background Mode:

I configured the service with isForegroundMode: false to avoid the notification, but this causes the service to be stopped by the Android system when the app goes into the background.

await service.configure(
  androidConfiguration: AndroidConfiguration(
    onStart: onStart,
    autoStart: true,
    isForegroundMode: false,  // No notification, but the service may be killed by the system
  ),
  iosConfiguration: IosConfiguration(
    autoStart: true,
    onForeground: onStart,
    onBackground: onIosBackground,
  ),
);

Using a Low-Importance Notification:

I tried setting up a notification with Importance.min, hoping it would be less visible, but the notification is still displayed.

const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'low_importance_channel',
  'Low Importance Notifications',
  description: 'This channel is used for low importance notifications',
  importance: Importance.min,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.initialize(
  const InitializationSettings(
    android: AndroidInitializationSettings('ic_bg_service_small'),
  ),
);

Despite my efforts, the notification still shows up when the service is running in the foreground. I need the service to run continuously in the background or foreground without displaying a persistent notification.

Upvotes: 0

Views: 146

Answers (0)

Related Questions