1.0.1 • Published 2 years ago

react-native-background-timer-workmanager v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Native Background Timer Workmanager Android

Emit event periodically in both foreground and background.

Installation

  1. If you use Expo to create a project you'll just need to "eject".

    expo eject
  2. 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
  3. 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
  4. 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

    ```diff
    + 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;