react-native-background-timer-workmanager
v1.0.1
Published
Emit event periodically in both foreground and background
Downloads
2
Maintainers
Readme
React Native Background Timer Workmanager [Android]
Emit event periodically in both foreground and background.
Installation
If you use Expo to create a project you'll just need to "eject".
expo eject
Install React Native Background Timer Work Manager package.
yarn add react-native-background-timer-workmanager # or using npm npm install react-native-background-timer-workmanager --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-workmanager
Link the library manually if you get errors:
android/settings.gradle
+ include ':react-native-background-timer-workmanager' + project(':react-native-background-timer-workmanager').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-timer-workmanager/android')
android/app/build.gradle
dependencies { + implementation project(':react-native-background-timer-workmanager') }
android/app/src/main/java/com/your-app/MainApplication.java
+ import com.shubhamd99.timer.BackgroundTimerPackage; @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( + new BackgroundTimerPackage() ); }
Usage
import BackgroundTimer from 'react-native-background-timer-workmanager';
BackgroundTimer.start(3000, 'UNIQUE_TAG' () => {
// code that will be called every 3 seconds
});
BackgroundTimer.stop('UNIQUE_TAG'); // To Stop Polling
Example:
import React, {useEffect} from 'react';
const App = () => {
useEffect(() => {
BackgroundTimer.start(10000, 'homeScreenPolling', () => {
console.log('polling..');
});
return () => BackgroundTimer.stop('homeScreenPolling');
}, []);
};
export default App;