Please support my work and support continuous development by your donation.
Special React Native fork of cordova-plugin-mauron85-background-geolocation.
Repository react-native-background-geolocation-example is hosting example app for both iOS and Android platform.
import React, { Component } from 'react';
import BackgroundGeolocation from 'react-native-mauron85-background-geolocation';
class BgTracking extends Component {
componentWillMount() {
BackgroundGeolocation.configure({
desiredAccuracy: 10,
stationaryRadius: 50,
distanceFilter: 50,
locationTimeout: 30,
notificationTitle: 'Background tracking'
89D9
,
notificationText: 'enabled',
debug: true,
startOnBoot: false,
stopOnTerminate: false,
locationProvider: 1, // 0 => ANDROID_DISTANCE_FILTER_PROVIDER | 1 => ANDROID_ACTIVITY_PROVIDER
interval: 10000,
fastestInterval: 5000,
activitiesInterval: 10000,
stopOnStillActivity: false,
url: 'http://192.168.81.15:3000/location',
httpHeaders: {
'X-FOO': 'bar'
}
});
BackgroundGeolocation.on('location', (location) => {
//handle your locations here
Actions.sendLocation(location);
});
BackgroundGeolocation.on('error', (error) => {
console.log('[ERROR] BackgroundGeolocation error:', error);
});
BackgroundGeolocation.start(() => {
console.log('[DEBUG] BackgroundGeolocation started successfully');
});
}
}
export default BgTracking;
Add package to your project
npm install react-native-mauron85-background-geolocation --save
In android/settings.gradle
...
include ':react-native-mauron85-background-geolocation', ':app'
project(':react-native-mauron85-background-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-mauron85-background-geolocation/android')
...
In android/app/build.gradle
dependencies {
...
compile project(':react-native-mauron85-background-geolocation')
...
}
Register module (in MainActivity.java
)
import com.marianhello.react.BackgroundGeolocationPackage; // <--- Import Package
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
...
/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new BackgroundGeolocationPackage() // <---- Add the Package
);
}
...
}
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- add
./node_modules/react-native-mauron85-background-geolocation/ios/RCTBackgroundGeolocation.xcodeproj
- In the XCode project navigator, select your project, select the
Build Phases
tab and in theLink Binary With Libraries
section add libRCTBackgroundGeolocation.a - add
UIBackgroundModes
location toInfo.plist
- add
NSLocationAlwaysUsageDescription
App requires background tracking toInfo.plist