@capgo/capacitor-background-task 사용
Capacitor 앱에서 이름이 지정된 작업, 지속적인 등록, 상태 확인, 등록 해제 및 개발 트리거와 함께 주기적인 배경 fetch 작업을 설정합니다.
설치
npm install @capgo/capacitor-background-task
npx cap sync
iOS 설정
이름이 지정된 작업을 위한 JavaScript 콜백을 정의하고, 주기적인 작업을 저장하고 스케줄링하고, 하나의 작업을 취소하고, 하나의 작업의 등록 상태를 확인합니다. 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
iOS 설정
defineTask배경 처리 지원을 추가하여registerTaskAsync그런 다음 실행하십시오.unregisterTaskAsync이 플러그인은 다음을 제공합니다.isTaskRegisteredAsync- 이름이 지정된 작업에 대한 JavaScript 콜백을 정의합니다.getRegisteredTasksAsync- 등록된 작업 이름 목록을 표시합니다.getStatusAsync- 네이티브 배경 작업 사용 가능 여부를 확인합니다.triggerTaskWorkerForTestingAsync- 개발 환경에서 등록된 작업을 즉시 실행합니다.addExpirationListener- iOS에서 작업이 만료될 때 반응합니다.
Example Usage
모듈 스코프에서 작업을 정의하여 OS가 앱을 깨울 때 작업이 사용 가능하도록 합니다.
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;
}
});
await BackgroundTask.registerTaskAsync(SYNC_TASK, {
minimumInterval: 30,
requiresNetwork: true,
});
작업 상태 및 등록된 작업 이름을 확인합니다.
const status = await BackgroundTask.getStatusAsync();
const tasks = await BackgroundTask.getRegisteredTasksAsync();
console.log({ status, tasks });
개발 환경에서 작업을 실행합니다.
await BackgroundTask.triggerTaskWorkerForTestingAsync();
작업을 취소합니다.
await BackgroundTask.unregisterTaskAsync(SYNC_TASK);
Platform Notes
- 배경 작업 스케줄은 정확한 타이머가 아닌 기회주의적입니다.
- Android의 주기적 작업은 15분의 최소 간격을 가지고 있습니다.
- iOS는 가장 빠른 시작 날짜로 처리하고 나중에 실행될 수 있습니다.
minimumInterval물리 장치에서 iOS 배경 처리를 테스트하세요. - Full Reference
__CAPGO_KEEP_0__
- https://GitHub.com/Cap-go/__CAPGO_KEEP_1__-background-task/ https://github.com/Cap-go/capacitor-background-task/
- Using @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-background-task
Keep going from Using @capgo/capacitor-background-task
Using @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-background-task Using @capgo/capacitor-background-task @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-background-task @capgo/capacitor-background-task capgo/capacitor-background-task 구현 세부 사항에 대해 Getting Started __CAPGO_KEEP_0__ Plugin Directory 구현 세부 사항에 대해 Capgo Plugin Directory Capgo Plugin Directory에서 __CAPGO_KEEP_1__에 의해 제공되는 플러그인 Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, and __CAPGO_KEEP_0__ Plugin Directory 구현 세부 사항에 대해 Footer