React Native Background Timer
Emit event periodically (even when app is in the background).
Installation
-
If you use Expo to create a project you'll just need to "eject".
expo eject
-
Install React Native Background Timer package.
yarn add react-native-background-timer
npm install react-native-background-timer --save
-
Link React Native Background Timer library. This step is not necessary when you use React Native >= 0.60 (and your app is not ejected from Expo).
react-native link react-native-background-timer
-
If you use CocoaPods or React Native >= 0.60 (and your app is not ejected from Expo) or your app is ejected from Expo, then before running your app on iOS, make sure you have CocoaPods installed and run:
cd ios
pod install
Link the library manually if you get errors:
- Android:
TypeError: Cannot read property 'setTimeout' of undefined
or TypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout')
- iOS:
Native module cannot be null
Android manual linking
-
android/settings.gradle
+ include ':react-native-background-timer'
+ project(':react-native-background-timer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-timer/android')
-
android/app/build.gradle
dependencies {
+ implementation project(':react-native-background-timer')
}
-
android/app/src/main/java/com/your-app/MainApplication.java
+ import com.ocetnik.timer.BackgroundTimerPackage;
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
+ new BackgroundTimerPackage()
);
}
iOS manual linking
Usage
import BackgroundTimer from 'react-native-background-timer';
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(() => {
},
3000);
BackgroundTimer.stopBackgroundTimer();
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();
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.
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.
const intervalId = BackgroundTimer.setInterval(() => {
console.log('tic');
}, 200);
BackgroundTimer.clearInterval(intervalId);
const timeoutId = BackgroundTimer.setTimeout(() => {
console.log('tac');
}, 10000);
BackgroundTimer.clearTimeout(timeoutId);
Obsolete
Obsolete usage which doesn't support 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,
})();
BackgroundTimer.start(5000);
EventEmitter.addListener('backgroundTimer', () => {
console.log('toe');
});
BackgroundTimer.stop();