npx expo install react-native-background-geolocation
npx expo install react-native-background-fetch
npx expo install expo-gradle-ext-vars
- Add the following three
plugins
:
{
"expo": {
"name": "your-app-name",
"plugins": [
+ [
+ "react-native-background-geolocation", {
+ "license": "YOUR_LICENSE_KEY_HERE"
+ }
+ ],
+ [
+ "expo-gradle-ext-vars", {
+ "googlePlayServicesLocationVersion": "21.1.0",
+ "appCompatVersion": "1.4.2"
+ }
+ ],
+ "react-native-background-fetch"
]
}
}
If you've purchased the Polygon Geofencing add-on for creating geofences of any shape, add your Polygon Geofencing license key:
{
"expo": {
"name": "your-app-name",
"plugins": [
[
"react-native-background-geolocation", {
"license": "YOUR_LICENSE_KEY_HERE",
+ "polygonLicense": "YOUR_POLYGON_LICENSE_HERE"
}
]
]
}
}
Warning
Polygon Geofencing support for Expo requires react-native-background-geolocation >= 4.16.4
.
If you've purchased an HMS Background Geolocation License for installing the plugin on Huawei devices without Google Play Services installed, add your HMS Background Geolocation license key:
{
"expo": {
"name": "your-app-name",
"plugins": [
[
"react-native-background-geolocation", {
"license": "YOUR_LICENSE_KEY_HERE",
+ "hmsLicense": "YOUR_HMS_LICENSE_HERE"
}
]
]
}
}
Warning
Huawei HMS support requires react-native-background-geolocation >= 3.11.0
.
- Add the following Usage Descriptions to the
ios.infoPlist
section: - These strings are used by the OS when requesting permission from the user. It's up to you to re-write these strings as-desired.
{
"expo": {
"name": "your-app-name",
.
.
.
"ios": {
+ "infoPlist": {
+ "NSLocationAlwaysAndWhenInUseUsageDescription": "[CHANGEME] This app requires location in the background",
+ "NSLocationWhenInUseUsageDescription": "[CHANGEME] This app requires location while in use",
+ "NSMotionUsageDescription": "[CHANGEME] This app uses motion-detection to determine the motion-activity of the device (walking, vehicle, bicycle, etc)"
+ }
}
}
}
- If you configure the plugin with
locationAuthorizationRequest: 'WhenInUse'
, you can omitNSLocationAlwaysAndWhenInUseUsageDescription
- If you configure the plugin with
disableMotionActivityUpdates: true
, you can omitNSMotionUsageDescription
- Add the following
UIBackgroundModes
to theios.infoPlist
section:
{
"expo": {
"name": "your-app-name",
.
.
.
"ios": {
"infoPlist": {
"NSLocationAlwaysAndWhenInUseUsageDescription": "[CHANGEME] This app requires location in the background",
"NSLocationWhenInUseUsageDescription": "[CHANGEME] This app requires location while in use",
"NSMotionUsageDescription": "[CHANGEME] This app uses motion-detection to determine the motion-activity of the device (walking, vehicle, bicycle, etc)",
+ "UIBackgroundModes": [
+ "location",
+ "fetch",
+ "processing"
+ ],
+ "BGTaskSchedulerPermittedIdentifiers": [
+ "com.transistorsoft.fetch",
+ "com.transistorsoft.customtask"
+ ]
}
}
}
}
- While field-testing the plugin configured with
debug: true
(to enable debug found FX), you can optionally add theUIBackgroundMode
audio
so you can hear debug sounds while your app is in the background:
{
"expo": {
"name": "your-app-name",
.
.
.
"ios": {
"infoPlist": {
"NSLocationAlwaysAndWhenInUseUsageDescription": "[CHANGEME] This app requires location in the background",
"NSLocationWhenInUseUsageDescription": "[CHANGEME] This app requires location while in use",
"NSMotionUsageDescription": "[CHANGEME] This app uses motion-detection to determine the motion-activity of the device (walking, vehicle, bicycle, etc)",
"UIBackgroundModes": [
"location",
"fetch",
"processing",
+ "audio"
]
}
}
}
}
You must rebuild your Android app for the added plugins to be evaluated.
- If you're developing locally:
npx expo prebuild
Note
When using prebuild
, you must run your app as follows:
$ npx expo run:android
$ npx expo run:ios
- If you're using Expo EAS, you must first run
eas build
. - Adjust
--profile
as desired. - You must build ALL platforms, both iOS and Android:
eas build --profile development
Tip
react-native-background-fetch
is helpful for executing a periodic task (eg: every 15 minutes). You could use background-fetch
to periodically request the current location:
// Execute a task about every 15 minutes:
BackgroundFetch.configure({
minimumFetchInterval: 15
}, async (taskId) => { // <-- This is your periodic-task callback
const location = await BackgroundGeolocation.getCurrentPosition({
samples: 3,
extras: { // <-- your own arbitrary meta-data
"event": "getCurrentPosition"
}
});
console.log('[getCurrentPosition]', location);
BackgroundFetch.finish(taskId); // <-- signal that your task is complete
})