开始使用
复制一个包含安装步骤和本插件的完整Markdown指南的设置命令。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-background-task`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/background-task/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
@capgo/capacitor-background-task 让一个Capacitor应用程序注册名为周期性任务、缓存刷新、分析数据传递和其他后台获取操作的任务。
Install
包含安装、同步和源Markdown指南的内容。您可以使用我们的AI辅助设置来安装插件。使用以下命令将Capgo技能添加到您的AI工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-pluginscontext:Capgo营销网站.角色: UI标签.见于:页面代理/[slug].astro.消息键`agents_page_copy_label`(代理页面复制标签).
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-background-task` plugin in my project.然后使用以下提示:
bun add @capgo/capacitor-background-taskbunx cap synciOS 配置
iOS设置iOS设置部分标题: ios/App/App/Info.plist:
<key>UIBackgroundModes</key><array> <string>processing</string></array><key>BGTaskSchedulerPermittedIdentifiers</key><array> <string>app.capgo.backgroundtask.processing</string></array>然后运行:
bunx cap sync ios复制到剪贴板
导入import { BackgroundTask, BackgroundTaskResult } from '@capgo/capacitor-background-task';导入
定义一个任务定义一个任务
import { BackgroundTask, BackgroundTaskResult } from '@capgo/capacitor-background-task';
const SYNC_TASK = 'sync-offline-data';
BackgroundTask.defineTask(SYNC_TASK, async () => { try { await fetch('https://api.example.com/sync', { method: 'POST' }); return BackgroundTaskResult.Success; } catch { return BackgroundTaskResult.Failed; }});将回调函数定义在模块范围内,以便在操作系统启动应用程序时立即注册。
注册调度开始 registerTaskAsync 在您的应用程序有足够的上下文后启用后台作业。
await BackgroundTask.registerTaskAsync(SYNC_TASK, { minimumInterval: 30, requiresNetwork: true,});minimumInterval 时间单位为分钟。Android强制执行最小15分钟。iOS将值视为最早开始日期并可能稍后运行。
检查状态
状态检查const status = await BackgroundTask.getStatusAsync();const isRegistered = await BackgroundTask.isTaskRegisteredAsync(SYNC_TASK);const registeredTasks = await BackgroundTask.getRegisteredTasksAsync();
console.log({ status, isRegistered, registeredTasks });触发测试
触发测试在开发或QA环境中使用测试触发器。它会立即调用已注册的所有任务。
await BackgroundTask.triggerTaskWorkerForTestingAsync();await BackgroundTask.unregisterTaskAsync(SYNC_TASK);iOS可以在JavaScript工作完成之前停止一个后台任务。请在清理或检查点时监听失效事件。
const expiration = await BackgroundTask.addExpirationListener((event) => { console.warn('Background task expired', event.taskName, event.taskId);});
await expiration.remove();React Native后台任务兼容性
Section titled “React Native后台任务兼容性”插件还暴露了一个用于从 react-native-background-task.
import { BackgroundTask } from '@capgo/capacitor-background-task';
BackgroundTask.define(async () => { await fetch('https://api.example.com/sync', { method: 'POST' });});
await BackgroundTask.schedule({ period: 1800,});- 背景任务的调度是机会主义的,而不是精确的。
- 保持工作短小且幂等。
- 在返回之前持久化任何需要的状态。
BackgroundTaskResult.Success. - 避免依赖背景任务来实现用户可见的截止日期或闹钟。
继续从 Getting Started
标题为“继续从 Getting Started”如果您正在使用 开始使用 来规划原生插件工作,连接它与 使用 @capgo/capacitor-background-task 来使用 @capgo/capacitor-background-task 中的原生能力Capgo Plugin Directory 为产品工作流程在 Capgo 插件目录中 Capacitor 插件由 Capgo 为实现细节在 Capacitor 插件由 Capgo 添加或更新插件 为实现细节在添加或更新插件,并 Ionic 企业插件替代品 为产品工作流程在 Ionic 企业插件替代品。