@capawesome/capacitor-background-task

中文文档
8.0.3发布于 2026/09/02MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 6.5万

Capacitor plugin for running background tasks on iOS.

原始使用文档
文档目录26 节
  1. Use Cases
  2. Compatibility
  3. Installation
  4. Configuration
  5. Demo
  6. Usage
  7. API
  8. beforeExit(...)
  9. finish(...)
  10. Interfaces
  11. Type Aliases
  12. Quirks
  13. iOS
  14. Android
  15. FAQ
  16. How much time does my task have to run in the background?
  17. Is this plugin supported on Android?
  18. Can I run periodic or scheduled tasks with this plugin?
  19. How do I know when the app moves to the background?
  20. When do I have to call the finish method?
  21. Can I use this plugin with Ionic, React, Vue or Angular?
  22. Related Plugins
  23. Newsletter
  24. Changelog
  25. License
  26. Credits

Capacitor Background Task Plugin

Capacitor plugin for running background tasks.

Use Cases

The Background Task plugin is typically used to finish short-running work when the app moves to the background, for example:

  • Finish network requests: Complete an upload or API request that was started while the app was in the foreground.
  • Save application state: Persist unsaved user input or app state before the OS puts the app to sleep.
  • Sync data: Finish writing pending changes to your backend or local storage.
  • Clean up resources: Close connections or release resources gracefully before the app is suspended.

Compatibility

Plugin VersionCapacitor VersionStatus
8.x.x>=8.x.xActive support
7.x.x7.x.xDeprecated
6.x.x6.x.xDeprecated
5.x.x5.x.xDeprecated

Installation

You can use our AI-Assisted Setup to install the plugin. Add the Capawesome Skills to your AI tool using the following command:

npx skills add capawesome-team/skills --skill capacitor-plugins

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:

npm install @capawesome/capacitor-background-task
npx cap sync

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(...)

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 on Android and iOS.

ParamType
cb() => void

Returns: Promise<string>


finish(...)

finish(options: FinishOptions) => void

Finish the current background task. The OS will put the app to sleep.

Only available on Android and iOS.

ParamType
optionsFinishOptions

Interfaces

FinishOptions

PropType
taskIdCallbackID

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.

FAQ

How much time does my task have to run in the background?

On iOS, the task started with beforeExit(...) should be finished in less than 30 seconds. As soon as your work is done, call finish(...) so the OS can put the app to sleep. Read more about the underlying background execution mechanism in the Apple documentation.

Is this plugin supported on Android?

There is currently no ready implementation on Android, so the task is not extended there. Support for Android is planned for the future, see the Quirks section.

Can I run periodic or scheduled tasks with this plugin?

No, this plugin is designed to extend the execution time of your app when it moves to the background, so you can finish a short-running task. It does not schedule tasks that run while the app is closed.

How do I know when the app moves to the background?

Use the appStateChange listener of the official @capacitor/app plugin, as shown in the Usage section. When isActive becomes false, call beforeExit(...) to start the background task.

When do I have to call the finish method?

Call finish(...) with the task ID returned by beforeExit(...) as soon as everything is done. This tells the OS that the task is complete so it can put the app to sleep.

Can I use this plugin with Ionic, React, Vue or Angular?

Yes, the plugin is framework-agnostic. It works in any Capacitor app regardless of the web framework, including Ionic with Angular, React, or Vue, as well as plain JavaScript projects.

Newsletter

Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our Capawesome Newsletter.

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!

版本与发布记录

@capawesome/capacitor-background-task 共发布 11 个版本,首次发布于 2022/08/05;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.3latest2026/09/022 个月前
8.0.22026/02/228 个月前
8.0.12026/02/198 个月前
8.0.02025/12/1710 个月前
7.0.0-dev.1765885034dev2025/12/1610 个月前
7.0.12025/02/011.8 年前

关于 @capawesome/capacitor-background-task 的常见问题

@capawesome/capacitor-background-task 是什么?

Capacitor plugin for running background tasks on iOS.

如何安装 @capawesome/capacitor-background-task?

在项目目录运行 npm install @capawesome/capacitor-background-task,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capawesome/capacitor-background-task 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capawesome/capacitor-background-task 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capawesome/capacitor-background-task 可以商用吗?

@capawesome/capacitor-background-task 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capawesome/capacitor-background-task 的下载量有多少?

根据 npm 官方下载统计,@capawesome/capacitor-background-task 在 2026/10/09 – 2026/11/07 这 30 天内被下载 65,239 次,日均约 2,175 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capawesome/capacitor-background-task 还在维护吗?

最新版本 8.0.3 发布于 2026/09/02(2 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 5 个;首次发布于 2022/08/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。