Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
This library allows the scheduling of a single periodic task, which executes when the app is in the background or closed, no more frequently than every 15 minutes. Network, AsyncStorage etc can be used (anything except UI), so perfect for things like a background data sync for offline support.
Behind the scenes, this library takes a different approach with each platform:
Background Fetch technique.To achieve a unified API, this library exposes the lowest common denominator (e.g. only support for a single task, even though Android can support multiple).
For more per-platform flexibility, there are other platform-specific libraries with more granular customisation.
$ npm install react-native-background-task --save$ react-native link react-native-background-taskandroid/app/src/main/java/myapp/MainApplication.java, add the following to
the end of the onCreate() method:BackgroundTaskPackage.useContext(this);For iOS support, this library relies on version 2.0.x of react-native-background-fetch which can be installed as follows:
$ npm install [email protected] --save
$ react-native link react-native-background-fetchThis library will behave correctly on iOS as long as react-native-background-fetch
is installed alongside it, and has been linked with your project.
define(task)Define the JS code that this module should be executing.
Parameters:
task: required () => void - Function to be executed in the backgroundschedule(options)Specify the scheduling options for the task, and register it with the platform's scheduler.
componentDidMount). This is to avoid double-scheduling when the task
launches in headless mode.console.warn if the device is task was unable to be scheduled.Parameters:
options?: Object - Any configuration you want to be set with
the task. Note that most of these will only work on one platform.
period?: number - (Android only) Desired number of seconds between each
execution of the task. Even on Android, the OS will only take this as a
recommendation, and will enforce a minimum of 15 minutes (similar to iOS).
Default is 900 (15 minutes)timeout?: number - (Android only) Number of seconds the task will have
to execute. iOS has a hardcoded limit of 30 seconds. Default 30 seconds.finish()Must be called at the end of your task to indicate to the OS that it's finished. (Only required on iOS, no-op on Android).
cancel()Cancels any currently scheduled task.
statusAsync()Query the status of background tasks within this app. Returns a Promise with an object of the following shape:
available: boolean - Whether background tasks are available to the app.
On Android this will always be true, but on iOS background tasks could be
blocked (see UIBackgroundRefreshStatus).unavailableReason?: string - If unavailable, gives the reason:BackgroundTask.UNAVAILABLE_DENIED: - The user explicitly disabled
background behavior for this app or for the whole system.BackgroundTask.UNAVAILABLE_RESTRICTED: - Background updates
unavailable and can't be enabled by the user (e.g. parental controls).Android:
iOS:
statusAsync() to check for this).$ adb logcat *:S ReactNative:V ReactNativeJS:V BackgroundTask:VKeep in mind that after the app leaves the foreground, you'll have to wait at least 50% of the desired period (so, default is 7m30s) before the task executes.
A full example project can be found here: https://github.com/jamesisaac/RNBackgroundTaskExample
import React from 'react'
import { Text } from 'react-native'
import BackgroundTask from 'react-native-background-task'
BackgroundTask.define(() => {
console.log('Hello from a background task')
BackgroundTask.finish()
})
class MyApp extends React.Component {
componentDidMount() {
BackgroundTask.schedule()
}
render() {
return Hello world
}
}…No open issues yet, or sync has not completed.