Location tracking in react native even if app is closed?

Viewed 1420

I am looking for Location tracking in react native even if app is closed?

I have tried react-native-location but how i will track location and get response from gps when even app is closed. I am looking for google timeline functionality, location is even track when app is closed.

3 Answers

I had a similar issue where i need to contact an API after a certain time even if app is closed, So I used react-native-background-actions to fetch the API in a loop,you can do the similar

const trackLocation = async () => {
  await new Promise(async () => {
    for (let i = 1; BackgroundService.isRunning(); i++) {
      try {
        // depends on which lib you are using
        await getLocation();
      } catch (error) {
        // console.log(error);
      }
      await sleep(20000);
    }
  });
};

example - https://github.com/Rapsssito/react-native-background-actions#example-code

For such a requirement accomplishment, you should have to create a background task and also need to create a service in android to run up location tracking in the background state and even in the app's kill state.

For this, we can use the below libraries, with the combination of these two libraries the requirement can be implemented.

  1. https://github.com/transistorsoft/react-native-background-fetch
  2. https://github.com/Agontuk/react-native-geolocation-service

React native background fetch is to start the run of a specified task even if the app is in its kill state, and geolocation service to keep track of user location data.

Related