React Native Background Actions

170 Views Asked by At

I am developing a React native application. There is a request in the application that I need to send every 10 seconds. But I am trying to send this request when the application is in the background or closed.

I used the React-native-background-action package, but when I put it in the background, it stops the process. I would be happy if you can help.

1

There are 1 best solutions below

3
Pratik Prakash Bindage On
  1. First, install the package:

    npm install react-native-background-fetch
    
  2. import the package in your .js file

    import BackgroundFetch from 'react-native-background-fetch';
    
    componentDidMount() {
    BackgroundFetch.configure(
     {
       minimumFetchInterval: 10, // The task will be executed every 10 seconds
       stopOnTerminate: false, // The task will continue to run even if the app is terminated
       startOnBoot: true, // The task will start when the device is booted
     },
     () => {
       // Your code to send the request here
       console.log('Background task executed');
    
       // Remember to call BackgroundFetch.finish() to indicate that the task has completed
       BackgroundFetch.finish();
     },
     (error) => {
       console.log('Background task error', error);
     },
    );
    
    BackgroundFetch.start();
    }