Skip to content

BackgroundService

Keeps data collection running while the app is in the background on Android.

Runs an Android foreground service with a persistent notification. On iOS, background execution is not supported and all calls do nothing.

A singleton, accessed as BackgroundService(). Call initialize and then enable when the app starts. SmartPhoneClientManager.configure does this when enableBackgroundMode is true. Call disable to stop it.

Note that the background service must be enabled in the app's manifest file. You must specify the appropriate foregroundServiceType for your use case. See the Android docs and the list of available service types for more information on foreground service types.

Uses the flutter_background package.

Constructors

BackgroundService

BackgroundService()

Get the singleton BackgroundService.

Properties

isEnabled

bool get isEnabled

Whether background execution is currently enabled via enable.

Methods

disable

Future<bool> disable()

Disables background execution.

Returns true if background execution is now disabled, and false if initialize has not succeeded or disabling failed.

enable

Future<bool> enable()

Enables background execution.

Returns false if initialize has not succeeded, if the app lacks the background permissions, or if enabling fails. Returns true if already enabled.

initialize

Future<bool> initialize({
  1. String? notificationTitle,
  2. String? notificationText,
})

Initializes the background service. Must be called before enable.

On Android, this will initialize the background service with the provided notification title and text. If not provided, default English titles and text will be used. If you want to use localized titles and text, you can provide them here. The first call may show the Android "battery optimization" dialog.

On iOS, this will do nothing as background services are not supported.

Returns true if the background service was successfully initialized, false otherwise.