rn-background-timer-id
v2.4.3
Published
Emit event periodically (even when app is in the background)
Downloads
199
Maintainers
Readme
React Native Background Timer
- Emit event periodically (even when app is in the background).
- Same origin react-native-background-timer but fix duplicate id (can pass your id(number) or auto random from 100000000 - 999999999)
Installation
Install React Native Background Timer package.
yarn add rn-background-timer-id # or using npm npm install rn-background-timer-id --save
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
orTypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout')
- iOS:
Native module cannot be null
android/settings.gradle
+ include ':rn-background-timer-id' + project(':rn-background-timer-id').projectDir = new File(rootProject.projectDir, '../node_modules/rn-background-timer-id/android')
android/app/build.gradle
dependencies { + implementation project(':rn-background-timer-id') }
android/app/src/main/java/com/your-app/MainApplication.java
```diff + import com.ocetnik.timer.BackgroundTimerPackage; @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( + new BackgroundTimerPackage() ); } ```
ios/Podfile
```diff + pod 'rn-background-timer-id', :path => '../node_modules/rn-background-timer-id' ```
Usage
import BackgroundTimer from 'rn-background-timer-id';
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.
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.
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.
// 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 support multiple background timers.
import {
DeviceEventEmitter,
NativeAppEventEmitter,
Platform,
} from 'react-native';
import BackgroundTimer from 'rn-background-timer-id';
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();