Skip to content

Commit 070edf1

Browse files
committed
Readme updates
1 parent ef3c64b commit 070edf1

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

README.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
[![license](https://img.shields.io/github/license/jamesisaac/react-native-background-task.svg)](https://opensource.org/licenses/MIT)
55
[![npm downloads](https://img.shields.io/npm/dm/react-native-background-task.svg)](https://www.npmjs.com/package/react-native-background-task)
66

7-
**WORK IN PROGRESS / PLEASE TEST BEFORE DEPLOYING IN PRODUCTION**
8-
97
Periodic background tasks for React Native apps, cross-platform (iOS and
108
Android), which run even when the app is closed.
119

1210
This library allows the scheduling of a single periodic task, which executes
1311
when the app is in the background or closed, no more frequently than every 15
1412
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.
1614

1715
Behind the scenes, this library takes a different approach with each platform:
1816

@@ -23,7 +21,7 @@ Behind the scenes, this library takes a different approach with each platform:
2321
- **iOS**: [react-native-background-fetch](https://github.com/transistorsoft/react-native-background-fetch),
2422
which uses the iOS-specific `Background Fetch` technique.
2523

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
2725
(e.g. only support for a single task, even though Android can support multiple).
2826

2927
For more per-platform flexibility, there are other platform-specific libraries
@@ -37,7 +35,7 @@ $ npm install react-native-background-task --save
3735

3836
### Android
3937

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:
4139

4240
```bash
4341
$ react-native link react-native-background-task
@@ -53,7 +51,7 @@ $ npm install react-native-background-task --save
5351

5452
### iOS
5553

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
5755
[react-native-background-fetch](https://github.com/transistorsoft/react-native-background-fetch)
5856
which can be installed as follows:
5957

@@ -62,7 +60,7 @@ $ npm install [email protected] --save
6260
$ react-native link react-native-background-fetch
6361
```
6462

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`
6664
is installed alongside it, and has been linked with your project.
6765

6866
## API
@@ -78,7 +76,7 @@ Define the JS code that this module should be executing.
7876

7977
Parameters:
8078

81-
* **`task`**: **required** `() => void` - Function to be executed in the background
79+
- **`task`**: **required** `() => void` - Function to be executed in the background
8280

8381
### `schedule(options)`
8482

@@ -92,7 +90,7 @@ platform's scheduler.
9290

9391
Parameters:
9492

95-
- **`options`**: `?object` - Any configuration you want to be set with
93+
- **`options?`**: `Object` - Any configuration you want to be set with
9694
the task. Note that most of these will only work on one platform.
9795

9896
- **`period?`** `number` - (Android only) Desired number of seconds between each
@@ -125,6 +123,27 @@ an object of the following shape:
125123
- **`BackgroundTask.UNAVAILABLE_RESTRICTED`** - Background updates
126124
unavailable and can't be enabled by the user (e.g. parental controls).
127125

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+
128147
## Examples
129148

130149
### Simple
@@ -154,7 +173,7 @@ class MyApp extends React.Component {
154173

155174
```js
156175
import React from 'react'
157-
import { AsyncStorage, Button, Text } from 'react-native'
176+
import { Alert, AsyncStorage, Button } from 'react-native'
158177
import BackgroundTask from 'react-native-background-task'
159178

160179
BackgroundTask.define(async () => {
@@ -175,6 +194,25 @@ class MyApp extends React.Component {
175194
BackgroundTask.schedule({
176195
period: 1800, // Aim to run every 30 mins - more conservative on battery
177196
})
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+
}
178216
}
179217

180218
render() {

0 commit comments

Comments
 (0)