Emit event periodically (even when app is in the background)
Clone or download
Latest commit e930850 Nov 18, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Change node version in CircleCI config Aug 19, 2018
android Update to android/build.gradle Oct 28, 2018
ios Fixed background timer for iOS Nov 14, 2017
.eslintrc.js Fixed static method Aug 20, 2018
.gitignore Add eslint Aug 19, 2018
LICENSE Initial commit May 10, 2016
README.md Update README.md Aug 20, 2018
index.js Fix clearTimeout Nov 18, 2018
package.json Release v2.1.1 Nov 18, 2018
react-native-background-timer.podspec podspec: Add React dependency Jul 20, 2018
yarn.lock Add eslint Aug 19, 2018

README.md

React Native Background Timer

Emit event periodically (even when app is in the background).

Installation

⚠️ If you use create-react-native-app you must eject it before running react-native link.

  • yarn add react-native-background-timer
  • react-native link

Installation using CocoaPods on iOS

  • yarn add react-native-background-timer
  • add the following to your Podfile: pod 'react-native-background-timer', :path => '../node_modules/react-native-background-timer'

Usage Crossplatform

To use the same code both on Android and iOS use runBackgroundTimer() and stopBackgroundTimer(). There can be used only one background timer to keep code consistent.

BackgroundTimer.runBackgroundTimer(() => { 
//code that will be called every 3 seconds 
}, 
3000);
//rest of code will be performing for iOS on background too

BackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run.

Android didn't tested as well.

Usage iOS

After iOS update logic of background task little bit changed. So we can't use as it was. You have to use only start() and stop() without parameters. And all code that is performing will continue performing on background including all setTimeout() timers.

Example:

BackgroundTimer.start();
// Do whatever you want incuding setTimeout;
BackgroundTimer.stop();

If you call stop() on background no new tasks will be started! Don't call .start() twice, as it stop performing previous background task and starts new. If it will be called on backgound no tasks will run.

Usage Android

You can use the setInterval and setTimeout functions. This API is identical to that of react-native and can be used to quickly replace existing timers with background timers.

import BackgroundTimer from 'react-native-background-timer';
// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {
	// this will be executed every 200 ms
	// even when app is the the background
	console.log('tic');
}, 200);

// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
	// this will be executed once after 10 seconds
	// even when app is the the background
  	console.log('tac');
}, 10000);

// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);

Obsolete

Obsolete usage which doesn't allows to use multiple background timers.

import {
  DeviceEventEmitter,
  NativeAppEventEmitter,
  Platform,
} from 'react-native';

import BackgroundTimer from 'react-native-background-timer';
const EventEmitter = Platform.select({
  ios: () => NativeAppEventEmitter,
  android: () => DeviceEventEmitter,
})();
// start a global timer
BackgroundTimer.start(5000); // delay in milliseconds only for Android
// listen for event
EventEmitter.addListener('backgroundTimer', () => {
	// this will be executed once after 5 seconds
	console.log('toe');
});
// stop the timer
BackgroundTimer.stop();