Can you track background geolocation with React Native?

Yes is possible, but not using Expo, there are two modules that I've seen:

This is a comercial one, you have to buy a license https://github.com/transistorsoft/react-native-background-geolocation

And this https://github.com/mauron85/react-native-background-geolocation


The Expo Team release a new feature in SDK 32 that allow you tracking in background the location.

https://expo.canny.io/feature-requests/p/background-location-tracking


UPDATE 2019 EXPO 33.0.0:

Expo first deprecated it for their SDK 32.0.0 to meet app store guidelines but then reopened it in SDK 33.0.0.

Since, they have made it super easy to be able to implement background location. Use this code snippet that I used to make background geolocation work.

import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';

const LOCATION_TASK_NAME = 'background-location-task';

export default class Component extends React.Component {
  onPress = async () => {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
      accuracy: Location.Accuracy.Balanced,
      timeInterval: 5000,
    });
  };

  render() {
    return (
      <TouchableOpacity onPress={this.onPress} style={{marginTop: 100}}>
        <Text>Enable background location</Text>
      </TouchableOpacity>
    );
  }
}

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    alert(error)
    // Error occurred - check `error.message` for more details.
    return;
  }
  if (data) {
    const { locations } = data; 
    alert(JSON.stringify(locations); //will show you the location object
    //lat is locations[0].coords.latitude & long is locations[0].coords.longitude
    // do something with the locations captured in the background, possibly post to your server with axios or fetch API 
  }
});

The code works like a charm. One thing to note is that you cannot use geolocation in the Expo App. However, you can use it in your standalone build. Consequently, if you want to use background geolocation you have to use this code and then do expo build:ios and upload to the appstore in order to be able to get a users background location.

Additionally, note that you must include

"UIBackgroundModes":[
          "location",
          "fetch"
        ]

In the info.plist section of your app.json file.