@capawesome/capacitor-background-task¶
Capacitor plugin for running background tasks.
Newsletter¶
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our Capawesome Newsletter.
Compatibility¶
| Plugin Version | Capacitor Version | Status |
|---|---|---|
| 8.x.x | >=8.x.x | Active support |
| 7.x.x | 7.x.x | Deprecated |
| 6.x.x | 6.x.x | Deprecated |
| 5.x.x | 5.x.x | Deprecated |
Installation¶
You can use our AI-Assisted Setup to install the plugin. Add the Capawesome Skills to your AI tool using the following command:
Then use the following prompt:
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-background-task` plugin in my project.
If you prefer Manual Setup, install the plugin by running the following commands:
Configuration¶
No configuration required for this plugin.
Demo¶
A working example can be found here: robingenz/capacitor-plugin-demo
Usage¶
import { App } from '@capacitor/app';
import { BackgroundTask } from '@capawesome/capacitor-background-task';
App.addListener('appStateChange', async ({ isActive }) => {
if (isActive) {
return;
}
// The app state has been changed to inactive.
// Start the background task by calling `beforeExit`.
const taskId = await BackgroundTask.beforeExit(async () => {
// Run your code...
// Finish the background task as soon as everything is done.
BackgroundTask.finish({ taskId });
});
});
API¶
beforeExit(...)¶
Call this method when the app moves to the background. It allows the app to continue running a task in the background.
On iOS this method should be finished in less than 30 seconds.
Only available on Android and iOS.
| Param | Type |
|---|---|
cb |
() => void |
Returns: Promise<string>
finish(...)¶
Finish the current background task. The OS will put the app to sleep.
Only available on Android and iOS.
| Param | Type |
|---|---|
options |
FinishOptions |
Interfaces¶
FinishOptions¶
| Prop | Type |
|---|---|
taskId |
CallbackID |
Type Aliases¶
CallbackID¶
string
Quirks¶
iOS¶
On iOS the UIKit framework is used. Read more about the implementation and any limitations here.
Android¶
There is currently no ready implementation on Android. It's planned to add the support in the near future.
Changelog¶
See CHANGELOG.md.
License¶
See LICENSE.
Credits¶
This plugin is based on the Capacitor Background Task plugin. Thanks to everyone who contributed to the project!