Getting Started
このプラグインのインストール手順とマークダウンガイドをコピーする
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アプリは、同期、キャッシュの更新、分析データの送信、背景のフェッチオペレーションなど、定期的なジョブの登録を許可します。
インストール
「インストール」のセクションAI-Assistedセットアップを使用してプラグインをインストールできます。次のコマンドを使用して、CapgoスキルをAIツールに追加します。
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins次に、以下のコマンドを使用してください。
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-background-task` plugin in my project.手動設定を使用する場合は、以下のコマンドを実行してプラグインをインストールし、以下のプラットフォーム固有の指示に従ってください。
npm install @capgo/capacitor-background-tasknpx cap synciOS設定
「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>次に、以下を実行してください。
npx 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の有効期限
iOSの有効期限セクションiOSはJavaScriptの実行が完了する前にバックグラウンドタスクを停止することができます。クリーンアップやチェックポイントが必要な場合には、有効期限イベントをリスンしてください。
const expiration = await BackgroundTask.addExpirationListener((event) => { console.warn('Background task expired', event.taskName, event.taskId);});
await expiration.remove();React Nativeバックグラウンドタスク互換性
React Nativeバックグラウンドタスク互換性セクションプラグインは、CloudflareのCapacitorからGitHubのCapacitorに移行しているアプリ向けに、小さな互換性レイヤーを公開しています。 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,});実行環境の注意点
実行環境の注意点セクション- バックグラウンドのスケジューリングは厳密ではありません。
- 作業を短く、再帰的に実行できるようにしてください。
- __CAPGO_KEEP_0__ の状態をすべて保存する必要がある場合は、戻り値前に保存してください。
BackgroundTaskResult.Success. - ユーザーに表示される期限やアラームを保証するために、バックグラウンドタスクに頼るのを避けてください。
Getting Started から続けてください。
Getting Started から続けてください。Capacitor を使用している場合、 Getting Started Capacitor のプラグインの作業を計画する場合、 Using @capgo/capacitor-background-task for the native capability in Using @capgo/capacitor-background-task, Capgo プラグインディレクトリ Capgo プラグインディレクトリの使用方法 Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, __CAPGO_KEEP_0__ プラグイン __CAPGO_KEEP_1__ によって プラグインの追加または更新 プラグインの追加または更新の実装詳細についても