This project has been moved to the following monorepo: capawesome-team/capacitor-plugins.
@capawesome/capacitor-background-task
Capacitor plugin for running background tasks.
Maintainer | GitHub | Social |
---|---|---|
Robin Genz | robingenz | @robin_genz |
This is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.
npm install @capawesome/capacitor-background-task
npx cap sync
No configuration required for this plugin.
A working example can be found here: robingenz/capacitor-plugin-demo
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 });
});
});
beforeExit(cb: () => void) => Promise<CallbackID>
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 for Android and iOS.
Param | Type |
---|---|
cb |
() => void |
Returns: Promise<string>
finish(options: FinishOptions) => void
Finish the current background task. The OS will put the app to sleep.
Only available for Android and iOS.
Param | Type |
---|---|
options |
FinishOptions |
Prop | Type |
---|---|
taskId |
CallbackID |
string
On iOS the UIKit framework is used. Read more about the implementation and any limitations here.
There is currently no ready implementation on Android. It's planned to add the support in the near future.
See CHANGELOG.md.
See LICENSE.