All Projects → jamesisaac → React Native Background Task

jamesisaac / React Native Background Task

Licence: mit
Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to React Native Background Task

Apipeline
Feature-rich and pluggable offline-first API wrapper for all your javascript environements ! Easily wire-up your API and make your app work offline in minutes.
Stars: ✭ 92 (-89.46%)
Mutual labels:  fetch, offline
azure-sql-db-sync-api-change-tracking
Using Azure SQL Change Tracking API to Sync mobile Apps data with the Cloud
Stars: ✭ 58 (-93.36%)
Mutual labels:  sync, offline
Vault
Easy persistence of Contentful data for Android over SQLite.
Stars: ✭ 80 (-90.84%)
Mutual labels:  sync, offline
react-native-sync-adapter
Schedule background data synchronization using React Native.
Stars: ✭ 112 (-87.17%)
Mutual labels:  sync, background-jobs
react-sync
A declarative approach to fetching data via a React higher order component
Stars: ✭ 18 (-97.94%)
Mutual labels:  fetch, sync
Kinto.js
An Offline-First JavaScript Client for Kinto.
Stars: ✭ 268 (-69.3%)
Mutual labels:  sync, offline
Hangfire
An easy way to perform background job processing in your .NET and .NET Core applications. No Windows Service or separate process required
Stars: ✭ 7,126 (+716.27%)
Mutual labels:  background-jobs
Composable Fetch
A library that brings composition to http requests & solves most common tasks
Stars: ✭ 23 (-97.37%)
Mutual labels:  fetch
Imgcache.js
JS library based on the File API to cache images for offline recovery (target: cordova/phonegap & chrome)
Stars: ✭ 814 (-6.76%)
Mutual labels:  offline
Demo Progressive Web App
🎉 A demo for progressive web application with features like offline, push notifications, background sync etc,
Stars: ✭ 798 (-8.59%)
Mutual labels:  offline
Findergo
🐢 Open terminal quickly from Finder
Stars: ✭ 862 (-1.26%)
Mutual labels:  sync
Webstore
Package of applications allowing to compress HTML content into a single application together with a simple web browser (e-books alternative)
Stars: ✭ 7 (-99.2%)
Mutual labels:  offline
Smalldots
Stars: ✭ 905 (+3.67%)
Mutual labels:  fetch
Useful Softwares Tools List
List of useful Softwares, Tools, Plugins, Utilities (Online/Offline)
Stars: ✭ 7 (-99.2%)
Mutual labels:  offline
Realm Dotnet
Realm is a mobile database: a replacement for SQLite & ORMs
Stars: ✭ 927 (+6.19%)
Mutual labels:  sync
Pysyncdroid
A simple way to synchronize an Android device connected to a Linux PC via MTP over USB
Stars: ✭ 5 (-99.43%)
Mutual labels:  sync
Librg
🚀 Making multi-player gamedev simpler since 2017
Stars: ✭ 813 (-6.87%)
Mutual labels:  sync
Dexie.js
A Minimalistic Wrapper for IndexedDB
Stars: ✭ 7,337 (+740.44%)
Mutual labels:  offline
Sia Slice
Maintain disk images or other large files indefinitely on the Sia network.
Stars: ✭ 18 (-97.94%)
Mutual labels:  sync
Probtopdf
Turn online textbook into Exam-friendly, offline, searchable PDF
Stars: ✭ 27 (-96.91%)
Mutual labels:  offline

react-native-background-task

npm version license npm downloads

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:

  • Android: A native implementation, which provides scheduling on top of RN's built-in Headless JS (Android only).
    • Min API level: 16 (Android 4.1).
  • iOS: A proxy around react-native-background-fetch, which uses the iOS-specific 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.

Installation

$ npm install react-native-background-task --save

Android

  1. The linking of the library can be done automatically by running:
$ react-native link react-native-background-task
  1. One manual step is still needed - in your project file android/app/src/main/java/myapp/MainApplication.java, add the following to the end of the onCreate() method:
BackgroundTaskPackage.useContext(this);

iOS

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-fetch

This library will behave correctly on iOS as long as react-native-background-fetch is installed alongside it, and has been linked with your project.

API

define(task)

Define the JS code that this module should be executing.

  • Should be called at the top level of your JS, not inside a component. This is because in headless mode no components are mounted, but the code still needs to be accessible.
  • Will overwrite any previously defined task.

Parameters:

  • task: required () => void - Function to be executed in the background

schedule(options)

Specify the scheduling options for the task, and register it with the platform's scheduler.

  • Should be called from inside a component (e.g. your App's componentDidMount). This is to avoid double-scheduling when the task launches in headless mode.
  • Will 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).

Limitations

  • Tasks cannot be scheduled any more frequently than every 15 minutes.
  • The exact timings of task execution are unpredictable, as both Anrdoid and iOS use black-box algorithms, which depend on factors like device sleep state. This library should only be used for tasks that are an incremental feature, and can have inexact timing, such as the periodic background syncing of data. You should be prepared for the case that background task doesn't fires at all.

Android:

  • Tasks will not run while the app is in the foreground, and scheduling can be made even more imprecise when the app goes in/out of the foreground (as tasks are rescheduled as soon as the app goes into the background).

iOS:

  • iOS Background Fetch will not continue to run after a user manually closes the app. (The app being closed by the OS to free up memory etc. should be fine).
  • Background tasks will not be scheduled in the simulator. You'll need to either test it on a real device, or use the Simulate Background Fetch feature of Xcode.
  • The user can disable Background App Refresh for your app from their Settings (use statusAsync() to check for this).

Debugging

Android

$ adb logcat *:S ReactNative:V ReactNativeJS:V BackgroundTask:V

Keep 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.

Examples

A full example project can be found here: https://github.com/jamesisaac/RNBackgroundTaskExample

Simple

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 <Text>Hello world</Text>
  }
}

Fetch / store data

import React from 'react'
import { Alert, AsyncStorage, Button } from 'react-native'
import BackgroundTask from 'react-native-background-task'

BackgroundTask.define(async () => {
  // Fetch some data over the network which we want the user to have an up-to-
  // date copy of, even if they have no network when using the app
  const response = await fetch('http://feeds.bbci.co.uk/news/rss.xml')
  const text = await response.text()
  
  // Data persisted to AsyncStorage can later be accessed by the foreground app
  await AsyncStorage.setItem('@MyApp:key', text)
  
  // Remember to call finish()
  BackgroundTask.finish()
})

class MyApp extends React.Component {
  componentDidMount() {
    BackgroundTask.schedule({
      period: 1800, // Aim to run every 30 mins - more conservative on battery
    })
    
    // Optional: Check if the device is blocking background tasks or not
    this.checkStatus()
  }
  
  async checkStatus() {
    const status = await BackgroundTask.statusAsync()
    
    if (status.available) {
      // Everything's fine
      return
    }
    
    const reason = status.unavailableReason
    if (reason === BackgroundTask.UNAVAILABLE_DENIED) {
      Alert.alert('Denied', 'Please enable background "Background App Refresh" for this app')
    } else if (reason === BackgroundTask.UNAVAILABLE_RESTRICTED) {
      Alert.alert('Restricted', 'Background tasks are restricted on your device')
    }
  }
  
  render() {
    return (
      <View>
        <Button
          title="Read results from AsyncStorage"
          onPress={async () => {
            const result = await AsyncStorage.getItem('@MyApp:key')
            console.log(result) 
          }}
        />
      </View>
    )
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].