4
4
[ ![ license] ( https://img.shields.io/github/license/jamesisaac/react-native-background-task.svg )] ( https://opensource.org/licenses/MIT )
5
5
[ ![ npm downloads] ( https://img.shields.io/npm/dm/react-native-background-task.svg )] ( https://www.npmjs.com/package/react-native-background-task )
6
6
7
- ** WORK IN PROGRESS / PLEASE TEST BEFORE DEPLOYING IN PRODUCTION**
8
-
9
7
Periodic background tasks for React Native apps, cross-platform (iOS and
10
8
Android), which run even when the app is closed.
11
9
12
10
This library allows the scheduling of a single periodic task, which executes
13
11
when the app is in the background or closed, no more frequently than every 15
14
12
minutes. Network, AsyncStorage etc can be used (anything except UI), so
15
- perfect for things like a background data sync.
13
+ perfect for things like a background data sync for offline support .
16
14
17
15
Behind the scenes, this library takes a different approach with each platform:
18
16
@@ -23,7 +21,7 @@ Behind the scenes, this library takes a different approach with each platform:
23
21
- ** iOS** : [ react-native-background-fetch] ( https://github.com/transistorsoft/react-native-background-fetch ) ,
24
22
which uses the iOS-specific ` Background Fetch ` technique.
25
23
26
- To achieve a unified API, this package exposes the lowest common denominator
24
+ To achieve a unified API, this library exposes the lowest common denominator
27
25
(e.g. only support for a single task, even though Android can support multiple).
28
26
29
27
For more per-platform flexibility, there are other platform-specific libraries
@@ -37,7 +35,7 @@ $ npm install react-native-background-task --save
37
35
38
36
### Android
39
37
40
- 1 . The linking of the package can be done automatically by running:
38
+ 1 . The linking of the library can be done automatically by running:
41
39
42
40
``` bash
43
41
$ react-native link react-native-background-task
@@ -53,7 +51,7 @@ $ npm install react-native-background-task --save
53
51
54
52
### iOS
55
53
56
- For iOS support, this package relies on version 2.0.x of
54
+ For iOS support, this library relies on version 2.0.x of
57
55
[ react-native-background-fetch] ( https://github.com/transistorsoft/react-native-background-fetch )
58
56
which can be installed as follows:
59
57
62
60
$ react-native link react-native-background-fetch
63
61
```
64
62
65
- This package will behave correctly on iOS as long as ` react-native-background-fetch `
63
+ This library will behave correctly on iOS as long as ` react-native-background-fetch `
66
64
is installed alongside it, and has been linked with your project.
67
65
68
66
## API
@@ -78,7 +76,7 @@ Define the JS code that this module should be executing.
78
76
79
77
Parameters:
80
78
81
- * ** ` task ` ** : ** required** ` () => void ` - Function to be executed in the background
79
+ - ** ` task ` ** : ** required** ` () => void ` - Function to be executed in the background
82
80
83
81
### ` schedule(options) `
84
82
@@ -92,7 +90,7 @@ platform's scheduler.
92
90
93
91
Parameters:
94
92
95
- - ** ` options ` ** : ` ?object ` - Any configuration you want to be set with
93
+ - ** ` options? ` ** : ` Object ` - Any configuration you want to be set with
96
94
the task. Note that most of these will only work on one platform.
97
95
98
96
- ** ` period? ` ** ` number ` - (Android only) Desired number of seconds between each
@@ -125,6 +123,27 @@ an object of the following shape:
125
123
- ** ` BackgroundTask.UNAVAILABLE_RESTRICTED ` ** - Background updates
126
124
unavailable and can't be enabled by the user (e.g. parental controls).
127
125
126
+ ## Caveats
127
+
128
+ - The exact timings of tasks are unpredictable (depends on device sleep state
129
+ etc.), and cannot be more frequent than every 15 minutes. This library
130
+ should only be used for tasks that can have inexact timing, such as the
131
+ periodic background syncing of data.
132
+
133
+ Android:
134
+
135
+ - Tasks will not run while the app is in the foreground, and scheduling can be
136
+ made even more imprecise when the app goes in/out of the foreground (as tasks
137
+ are rescheduled as soon as the app goes into the background).
138
+
139
+ iOS:
140
+
141
+ - iOS Background Fetch does not work in the simulator. It must be tested on a
142
+ real device.
143
+ - The user can disable Background App Refresh for your app from their Settings
144
+ (use ` statusAsync() ` to check for this).
145
+
146
+
128
147
## Examples
129
148
130
149
### Simple
@@ -154,7 +173,7 @@ class MyApp extends React.Component {
154
173
155
174
``` js
156
175
import React from ' react'
157
- import { AsyncStorage , Button , Text } from ' react-native'
176
+ import { Alert , AsyncStorage , Button } from ' react-native'
158
177
import BackgroundTask from ' react-native-background-task'
159
178
160
179
BackgroundTask .define (async () => {
@@ -175,6 +194,25 @@ class MyApp extends React.Component {
175
194
BackgroundTask .schedule ({
176
195
period: 1800 , // Aim to run every 30 mins - more conservative on battery
177
196
})
197
+
198
+ // Optional: Check if the device is blocking background tasks or not
199
+ this .checkStatus ()
200
+ }
201
+
202
+ async checkStatus () {
203
+ const status = await BackgroundTask .statusAsync ()
204
+
205
+ if (status .available ) {
206
+ // Everything's fine
207
+ return
208
+ }
209
+
210
+ const reason = status .unavailableReason
211
+ if (reason === BackgroundTask .UNAVAILABLE_DENIED ) {
212
+ Alert .alert (' Denied' , ' Please enable background "Background App Refresh" for this app' )
213
+ } else if (reason === BackgroundTask .UNAVAILABLE_RESTRICTED ) {
214
+ Alert .alert (' Restricted' , ' Background tasks are restricted on your device' )
215
+ }
178
216
}
179
217
180
218
render () {
0 commit comments