@capgo/capacitor-background-task 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,421 @@
1
+ # @capgo/capacitor-background-task
2
+
3
+ <a href="https://capgo.app/"><img src="https://capgo.app/readme-banner.svg?repo=Cap-go/capacitor-background-task" alt="Capgo - Instant updates for Capacitor" /></a>
4
+
5
+ <div align="center">
6
+ <h2><a href="https://capgo.app/?ref=plugin_background_task">Get instant updates for your app with Capgo</a></h2>
7
+ <h2><a href="https://capgo.app/consulting/?ref=plugin_background_task">Need a plugin feature? We can build it</a></h2>
8
+ </div>
9
+
10
+ Periodic background task scheduling for Capacitor apps. It follows the practical feature set of Expo BackgroundTask: named tasks, persistent registration, status checks, unregistering, a testing trigger, and iOS expiration events.
11
+
12
+ ## What It Does
13
+
14
+ - Schedules periodic background work on Android with WorkManager.
15
+ - Schedules background processing on iOS with BGTaskScheduler.
16
+ - Supports multiple named tasks with `minimumInterval` in minutes.
17
+ - Emits retained task events so task runs recorded before JavaScript is ready can be drained.
18
+ - Provides a small `react-native-background-task` compatible API: `define`, `schedule`, `cancel`, `statusAsync`, and `finish`.
19
+
20
+ ## Limits
21
+
22
+ - Background tasks are not exact timers. Android and iOS decide when work actually runs.
23
+ - Android enforces a 15 minute minimum interval.
24
+ - iOS may delay runs substantially based on battery, network, and user behavior.
25
+ - iOS background tasks do not run in the simulator; use a physical device.
26
+ - This plugin cannot make an app run indefinitely in the background.
27
+
28
+ ## Compatibility
29
+
30
+ | Plugin version | Capacitor compatibility | Maintained |
31
+ | -------------- | ----------------------- | ---------- |
32
+ | v8.\*.\* | v8.\*.\* | ✅ |
33
+ | v7.\*.\* | v7.\*.\* | On demand |
34
+ | v6.\*.\* | v6.\*.\* | On demand |
35
+
36
+ Policy:
37
+
38
+ - New plugins start at version `8.0.0` (Capacitor 8 baseline).
39
+ - Backward compatibility for older Capacitor majors is supported on demand.
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ npm install @capgo/capacitor-background-task
45
+ npx cap sync
46
+ ```
47
+
48
+ ## iOS Setup
49
+
50
+ Add the background processing mode and permitted task identifier to `ios/App/App/Info.plist`:
51
+
52
+ ```xml
53
+ <key>UIBackgroundModes</key>
54
+ <array>
55
+ <string>processing</string>
56
+ </array>
57
+ <key>BGTaskSchedulerPermittedIdentifiers</key>
58
+ <array>
59
+ <string>app.capgo.backgroundtask.processing</string>
60
+ </array>
61
+ ```
62
+
63
+ ## Usage
64
+
65
+ Define tasks at module scope so they are available as soon as the app is started by the OS.
66
+
67
+ ```typescript
68
+ import { BackgroundTask, BackgroundTaskResult } from '@capgo/capacitor-background-task';
69
+
70
+ const SYNC_TASK = 'sync-offline-data';
71
+
72
+ BackgroundTask.defineTask(SYNC_TASK, async () => {
73
+ try {
74
+ await fetch('https://example.com/sync', { method: 'POST' });
75
+ return BackgroundTaskResult.Success;
76
+ } catch {
77
+ return BackgroundTaskResult.Failed;
78
+ }
79
+ });
80
+
81
+ await BackgroundTask.registerTaskAsync(SYNC_TASK, {
82
+ minimumInterval: 30,
83
+ requiresNetwork: true,
84
+ });
85
+ ```
86
+
87
+ ## Testing
88
+
89
+ ```typescript
90
+ await BackgroundTask.triggerTaskWorkerForTestingAsync();
91
+ ```
92
+
93
+ ## React Native Compatibility
94
+
95
+ ```typescript
96
+ import { BackgroundTask } from '@capgo/capacitor-background-task';
97
+
98
+ BackgroundTask.define(async () => {
99
+ await fetch('https://example.com/sync', { method: 'POST' });
100
+ });
101
+
102
+ await BackgroundTask.schedule({
103
+ period: 1800,
104
+ });
105
+ ```
106
+
107
+ ## Example App
108
+
109
+ The `example-app/` folder is linked via `file:..` and is intended for validating native wiring during development.
110
+
111
+ ## API
112
+
113
+ <docgen-index>
114
+
115
+ * [`defineTask(...)`](#definetask)
116
+ * [`registerTaskAsync(...)`](#registertaskasync)
117
+ * [`unregisterTaskAsync(...)`](#unregistertaskasync)
118
+ * [`isTaskRegisteredAsync(...)`](#istaskregisteredasync)
119
+ * [`getRegisteredTasksAsync()`](#getregisteredtasksasync)
120
+ * [`getPendingTaskRunsAsync()`](#getpendingtaskrunsasync)
121
+ * [`getStatusAsync()`](#getstatusasync)
122
+ * [`triggerTaskWorkerForTestingAsync()`](#triggertaskworkerfortestingasync)
123
+ * [`addExpirationListener(...)`](#addexpirationlistener)
124
+ * [`define(...)`](#define)
125
+ * [`schedule(...)`](#schedule)
126
+ * [`cancel()`](#cancel)
127
+ * [`statusAsync()`](#statusasync)
128
+ * [`finish(...)`](#finish)
129
+ * [Interfaces](#interfaces)
130
+ * [Type Aliases](#type-aliases)
131
+ * [Enums](#enums)
132
+
133
+ </docgen-index>
134
+
135
+ <docgen-api>
136
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
137
+
138
+ ### defineTask(...)
139
+
140
+ ```typescript
141
+ defineTask(taskName: string, callback: BackgroundTaskCallback) => void
142
+ ```
143
+
144
+ Define the JavaScript callback for a task. Call this at module/global scope.
145
+
146
+ | Param | Type |
147
+ | -------------- | ------------------------------------------------------------------------- |
148
+ | **`taskName`** | <code>string</code> |
149
+ | **`callback`** | <code><a href="#backgroundtaskcallback">BackgroundTaskCallback</a></code> |
150
+
151
+ --------------------
152
+
153
+
154
+ ### registerTaskAsync(...)
155
+
156
+ ```typescript
157
+ registerTaskAsync(taskName: string, options?: BackgroundTaskOptions | undefined) => Promise<void>
158
+ ```
159
+
160
+ Register a named periodic background task.
161
+
162
+ | Param | Type |
163
+ | -------------- | ----------------------------------------------------------------------- |
164
+ | **`taskName`** | <code>string</code> |
165
+ | **`options`** | <code><a href="#backgroundtaskoptions">BackgroundTaskOptions</a></code> |
166
+
167
+ --------------------
168
+
169
+
170
+ ### unregisterTaskAsync(...)
171
+
172
+ ```typescript
173
+ unregisterTaskAsync(taskName: string) => Promise<void>
174
+ ```
175
+
176
+ Unregister a named periodic background task.
177
+
178
+ | Param | Type |
179
+ | -------------- | ------------------- |
180
+ | **`taskName`** | <code>string</code> |
181
+
182
+ --------------------
183
+
184
+
185
+ ### isTaskRegisteredAsync(...)
186
+
187
+ ```typescript
188
+ isTaskRegisteredAsync(taskName: string) => Promise<boolean>
189
+ ```
190
+
191
+ Check whether a named task is registered.
192
+
193
+ | Param | Type |
194
+ | -------------- | ------------------- |
195
+ | **`taskName`** | <code>string</code> |
196
+
197
+ **Returns:** <code>Promise&lt;boolean&gt;</code>
198
+
199
+ --------------------
200
+
201
+
202
+ ### getRegisteredTasksAsync()
203
+
204
+ ```typescript
205
+ getRegisteredTasksAsync() => Promise<string[]>
206
+ ```
207
+
208
+ Return all registered task names.
209
+
210
+ **Returns:** <code>Promise&lt;string[]&gt;</code>
211
+
212
+ --------------------
213
+
214
+
215
+ ### getPendingTaskRunsAsync()
216
+
217
+ ```typescript
218
+ getPendingTaskRunsAsync() => Promise<BackgroundTaskEvent[]>
219
+ ```
220
+
221
+ Return pending task runs that native recorded before JavaScript was ready.
222
+
223
+ **Returns:** <code>Promise&lt;BackgroundTaskEvent[]&gt;</code>
224
+
225
+ --------------------
226
+
227
+
228
+ ### getStatusAsync()
229
+
230
+ ```typescript
231
+ getStatusAsync() => Promise<BackgroundTaskStatus>
232
+ ```
233
+
234
+ Return native background task availability.
235
+
236
+ **Returns:** <code>Promise&lt;<a href="#backgroundtaskstatus">BackgroundTaskStatus</a>&gt;</code>
237
+
238
+ --------------------
239
+
240
+
241
+ ### triggerTaskWorkerForTestingAsync()
242
+
243
+ ```typescript
244
+ triggerTaskWorkerForTestingAsync() => Promise<boolean>
245
+ ```
246
+
247
+ Trigger all registered tasks immediately for development/testing.
248
+
249
+ **Returns:** <code>Promise&lt;boolean&gt;</code>
250
+
251
+ --------------------
252
+
253
+
254
+ ### addExpirationListener(...)
255
+
256
+ ```typescript
257
+ addExpirationListener(listener: (event: BackgroundTaskEvent) => void) => Promise<PluginListenerHandle>
258
+ ```
259
+
260
+ Listen for iOS expiration callbacks.
261
+
262
+ | Param | Type |
263
+ | -------------- | --------------------------------------------------------------------------------------- |
264
+ | **`listener`** | <code>(event: <a href="#backgroundtaskevent">BackgroundTaskEvent</a>) =&gt; void</code> |
265
+
266
+ **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
267
+
268
+ --------------------
269
+
270
+
271
+ ### define(...)
272
+
273
+ ```typescript
274
+ define(callback: BackgroundTaskCallback) => void
275
+ ```
276
+
277
+ React Native background-task compatible single-task define helper.
278
+
279
+ | Param | Type |
280
+ | -------------- | ------------------------------------------------------------------------- |
281
+ | **`callback`** | <code><a href="#backgroundtaskcallback">BackgroundTaskCallback</a></code> |
282
+
283
+ --------------------
284
+
285
+
286
+ ### schedule(...)
287
+
288
+ ```typescript
289
+ schedule(options?: ReactNativeBackgroundTaskOptions | undefined) => Promise<void>
290
+ ```
291
+
292
+ React Native background-task compatible single-task scheduler.
293
+
294
+ | Param | Type |
295
+ | ------------- | --------------------------------------------------------------------------------------------- |
296
+ | **`options`** | <code><a href="#reactnativebackgroundtaskoptions">ReactNativeBackgroundTaskOptions</a></code> |
297
+
298
+ --------------------
299
+
300
+
301
+ ### cancel()
302
+
303
+ ```typescript
304
+ cancel() => Promise<void>
305
+ ```
306
+
307
+ React Native background-task compatible single-task cancel helper.
308
+
309
+ --------------------
310
+
311
+
312
+ ### statusAsync()
313
+
314
+ ```typescript
315
+ statusAsync() => Promise<ReactNativeBackgroundTaskStatus>
316
+ ```
317
+
318
+ React Native background-task compatible status helper.
319
+
320
+ **Returns:** <code>Promise&lt;<a href="#reactnativebackgroundtaskstatus">ReactNativeBackgroundTaskStatus</a>&gt;</code>
321
+
322
+ --------------------
323
+
324
+
325
+ ### finish(...)
326
+
327
+ ```typescript
328
+ finish(result?: BackgroundTaskResult | undefined) => Promise<void>
329
+ ```
330
+
331
+ React Native background-task compatible finish helper. Normal Expo-style
332
+ callbacks are finished automatically.
333
+
334
+ | Param | Type |
335
+ | ------------ | --------------------------------------------------------------------- |
336
+ | **`result`** | <code><a href="#backgroundtaskresult">BackgroundTaskResult</a></code> |
337
+
338
+ --------------------
339
+
340
+
341
+ ### Interfaces
342
+
343
+
344
+ #### BackgroundTaskEvent
345
+
346
+ Payload emitted when native scheduling asks JavaScript to run a task.
347
+
348
+ | Prop | Type | Description |
349
+ | --------------- | -------------------- | ----------------------------------------------------------------------------------------------------------- |
350
+ | **`taskName`** | <code>string</code> | Name passed to registerTaskAsync. |
351
+ | **`taskId`** | <code>string</code> | Native run identifier. The JavaScript wrapper finishes it automatically when the defined callback resolves. |
352
+ | **`timestamp`** | <code>number</code> | Native timestamp for the run. |
353
+ | **`test`** | <code>boolean</code> | True when triggered through triggerTaskWorkerForTestingAsync. |
354
+
355
+
356
+ #### BackgroundTaskOptions
357
+
358
+ Options for registering a periodic background task.
359
+
360
+ | Prop | Type | Description |
361
+ | --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
362
+ | **`minimumInterval`** | <code>number</code> | Inexact interval in minutes between task runs. Defaults to 720 minutes. Android enforces a 15 minute minimum. iOS treats this as an earliest begin date and may run much later. |
363
+ | **`requiresNetwork`** | <code>boolean</code> | Require an active network before running the native scheduler. Defaults to true. |
364
+
365
+
366
+ #### PluginListenerHandle
367
+
368
+ | Prop | Type |
369
+ | ------------ | ----------------------------------------- |
370
+ | **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |
371
+
372
+
373
+ #### ReactNativeBackgroundTaskOptions
374
+
375
+ React Native background-task compatible schedule options.
376
+
377
+ | Prop | Type | Description |
378
+ | ------------- | ------------------- | -------------------------------------------------------------------------- |
379
+ | **`period`** | <code>number</code> | Desired seconds between each execution. Mapped to minimumInterval minutes. |
380
+ | **`timeout`** | <code>number</code> | Android-only timeout hint kept for API compatibility. |
381
+
382
+
383
+ #### ReactNativeBackgroundTaskStatus
384
+
385
+ React Native background-task compatible status payload.
386
+
387
+ | Prop | Type | Description |
388
+ | ----------------------- | -------------------- | -------------------------------------------------- |
389
+ | **`available`** | <code>boolean</code> | Whether background tasks are available to the app. |
390
+ | **`unavailableReason`** | <code>string</code> | Reason when unavailable. |
391
+
392
+
393
+ ### Type Aliases
394
+
395
+
396
+ #### BackgroundTaskCallback
397
+
398
+ Function executed for a background task.
399
+
400
+ <code>(event: <a href="#backgroundtaskevent">BackgroundTaskEvent</a>): void | <a href="#backgroundtaskresult">BackgroundTaskResult</a> | Promise&lt;void | <a href="#backgroundtaskresult">BackgroundTaskResult</a>&gt;</code>
401
+
402
+
403
+ ### Enums
404
+
405
+
406
+ #### BackgroundTaskResult
407
+
408
+ | Members | Value | Description |
409
+ | ------------- | -------------- | ------------------------------- |
410
+ | **`Success`** | <code>1</code> | The task finished successfully. |
411
+ | **`Failed`** | <code>2</code> | The task failed. |
412
+
413
+
414
+ #### BackgroundTaskStatus
415
+
416
+ | Members | Value | Description |
417
+ | ---------------- | -------------- | -------------------------------------------------------- |
418
+ | **`Restricted`** | <code>1</code> | Background task scheduling is unavailable or restricted. |
419
+ | **`Available`** | <code>2</code> | Background task scheduling is available. |
420
+
421
+ </docgen-api>
@@ -0,0 +1,61 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
6
+ androidxWorkVersion = project.hasProperty('androidxWorkVersion') ? rootProject.ext.androidxWorkVersion : '2.10.5'
7
+ }
8
+
9
+ buildscript {
10
+ repositories {
11
+ google()
12
+ mavenCentral()
13
+ }
14
+ dependencies {
15
+ classpath 'com.android.tools.build:gradle:8.13.0'
16
+ }
17
+ }
18
+
19
+ apply plugin: 'com.android.library'
20
+
21
+ android {
22
+ namespace = "app.capgo.backgroundtask"
23
+ compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
24
+ defaultConfig {
25
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
26
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
27
+ versionCode 1
28
+ versionName "1.0"
29
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
30
+ }
31
+ buildTypes {
32
+ release {
33
+ minifyEnabled false
34
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
35
+ }
36
+ }
37
+ lint {
38
+ abortOnError = false
39
+ }
40
+ compileOptions {
41
+ sourceCompatibility JavaVersion.VERSION_21
42
+ targetCompatibility JavaVersion.VERSION_21
43
+ }
44
+ }
45
+
46
+ repositories {
47
+ google()
48
+ mavenCentral()
49
+ }
50
+
51
+
52
+ dependencies {
53
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
54
+ implementation project(':capacitor-android')
55
+ annotationProcessor project(':capacitor-android')
56
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
57
+ implementation "androidx.work:work-runtime:$androidxWorkVersion"
58
+ testImplementation "junit:junit:$junitVersion"
59
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
60
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
61
+ }
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>