Backgrounding & Workers
Backgrounding & Workers
Used for running actions in the device's background processes. All actions have a limit on how long they can run for. Depending on the device, actions can run for a max of 30 seconds up to 5 minutes.
This is all depending on the device's Watchdog (A program in the OS used to control and manage device memory).
To get started using it, you must use KmpBackgrounding.
Run your action inside a foreground service (for long running background processes)
This api will only work if the app is still running. It will trigger a service bound to the application, and (depending on the platform) can run indefinitely. Please note that once the app is closed, the service will be closed as well.
This is not suitable for background processing that needs to run beyond the scope of the app (meaning running even if the app is closed).
You can configure a title and message to use for the local notification bound to this service.
To use this API (ANDROID ONLY REQUIREMENT) you must enable a local notification icon. Please review KmpLocalNotification to get started.
_4KmpBackgrounding.createAndStartForegroundWorker(""){_4 // my action to run in the background_4 // this action will run even if the device is locked_4}
Run your action in the background without Configuration
_4KmpBackgrounding.createAndStartWorker(null){_4 // my action to run in the background_4 // this action will run even if the device is locked_4}
Run your action in the background with Configuration
You can control whether the background task will be allowed to run depending on whether the device has sufficient storage, battery, or other. To configure this please use BackgroundOptions
Below are the options available to choose from:
_5data class BackgroundOptions(_5 val requiresInternet: Boolean = false, // whether device must be connected to internet for background process to run_5 val requiresStorage: Boolean = false, // whether device must have sufficient storage in order to run background process_5 val requiresSufficientBattery: Boolean = false // whether device must have sufficient battery in order to run background process_5)
When ready invoke the function below:
_4KmpBackgrounding.createAndStartWorker(BackgroundOptions(requiresInternet = false, requiresStorage = false, requiresSufficientBattery = false)){_4 // my action to run in the background_4 // this action will run even if the device is locked_4}
Cancel Backgrounding
If you need to cancel the background tasks, you can invoke this function. This will cancel all the registered/running background tasks above:
_1KmpBackgrounding.cancelAllRunningWorkers()
Setup for Android
None Required
Setup for iOS
For iOS please add this into your info.plist file:
_4<key>UIBackgroundModes</key>_4<array> _4 <string>fetch</string>_4</array>