Skip to content

Getting Started

@capgo/capacitor-background-task lets a Capacitor app register named periodic jobs for sync, cache refreshes, analytics delivery, and other background fetch operations.

Terminal window
npm install @capgo/capacitor-background-task
npx cap sync

Add the background processing mode and permitted task identifier to ios/App/App/Info.plist:

<key>UIBackgroundModes</key>
<array>
<string>processing</string>
</array>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>app.capgo.backgroundtask.processing</string>
</array>

Then run:

Terminal window
npx cap sync ios
import { BackgroundTask, BackgroundTaskResult } from '@capgo/capacitor-background-task';

Define the callback at module scope so it is registered as soon as the app is started by the operating system.

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;
}
});

Call registerTaskAsync after your app has enough context to enable the background job.

await BackgroundTask.registerTaskAsync(SYNC_TASK, {
minimumInterval: 30,
requiresNetwork: true,
});

minimumInterval is in minutes. Android enforces a minimum of 15 minutes. iOS treats the value as an earliest begin date and may run later.

const status = await BackgroundTask.getStatusAsync();
const isRegistered = await BackgroundTask.isTaskRegisteredAsync(SYNC_TASK);
const registeredTasks = await BackgroundTask.getRegisteredTasksAsync();
console.log({ status, isRegistered, registeredTasks });

Use the test trigger in development or QA. It calls every registered task immediately.

await BackgroundTask.triggerTaskWorkerForTestingAsync();
await BackgroundTask.unregisterTaskAsync(SYNC_TASK);

iOS can stop a background task before your JavaScript work finishes. Listen for expiration events when cleanup or checkpointing matters.

const expiration = await BackgroundTask.addExpirationListener((event) => {
console.warn('Background task expired', event.taskName, event.taskId);
});
await expiration.remove();

React Native Background Task Compatibility

Section titled “React Native Background Task Compatibility”

The plugin also exposes a small compatibility layer for apps migrating from 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,
});
  • Background scheduling is opportunistic, not exact.
  • Keep work short and idempotent.
  • Persist any state you need before returning BackgroundTaskResult.Success.
  • Avoid relying on background tasks for user-visible deadlines or alarms.

If you are using Getting Started to plan native plugin work, connect it with Using @capgo/capacitor-background-task for the native capability in Using @capgo/capacitor-background-task, Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, Adding or Updating Plugins for the implementation detail in Adding or Updating Plugins, and Ionic Enterprise Plugin Alternatives for the product workflow in Ionic Enterprise Plugin Alternatives.