Run background service when application is closed
masterTo execute code even when the app is closed (e.g., for real-time data fetching):
- Foreground Mode (
isForegroundMode: true): Displays a notification according to Android policy. - Background Mode (
isForegroundMode: false): Requires running in release mode and requires disabling battery optimization so the service stays active when the user closes the application.
Use autoStart: true and autoStartOnBoot: true in AndroidConfiguration to ensure the service persists.
await service.configure(
androidConfiguration: AndroidConfiguration(
autoStart: true,
onStart: onStart,
isForegroundMode: false, // Requires disabling battery optimization in release mode
autoStartOnBoot: true,
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
onBackground: onIosBackground,
),
);