First, configure the plugin with the settings you require.
BackgroundGeolocation.configure({ locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER, desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY, stationaryRadius: 50, distanceFilter: 50, notificationTitle: 'Background tracking', notificationText: 'enabled', debug: true, interval: 10000, fastestInterval: 5000, activitiesInterval: 10000, url: 'http://192.168.81.15:3000/location', httpHeaders: { 'X-FOO': 'bar' }, // customize post properties postTemplate: { lat: '@latitude', lon: '@longitude', foo: 'bar' // you can also add your own properties } });
Then call start() to start location tracking.
start()
Note that all methods now return a Promise in case the success and fail callbacks are not provided, allowing the usage of async/await.
Promise
success
fail
async/await
We also recommend using the typescript definitions provided in this repo which have better chance of being up-to-date.
Configure options:
locationProvider
Number
desiredAccuracy
stationaryRadius
debug
Boolean
distanceFilter
stopOnTerminate
startOnBoot
interval
fastestInterval
activitiesInterval
stopOnStillActivity
notificationsEnabled
startForeground
notificationTitle
String
notificationText
notificationIconColor
notificationIconLarge
notificationIconSmall
activityType
pauseLocationUpdates
saveBatteryOnBackground
url
syncUrl
syncThreshold
httpHeaders
Object
maxLocations
postTemplate
Object\|Array
* DIS = DISTANCE_FILTER_PROVIDER ACT = ACTIVITY_PROVIDER RAW = RAW_PROVIDER
Partial reconfiguration is possible by later providing a subset of the configuration options:
BackgroundGeolocation.configure({ debug: true });
In this case new configuration options will be merged with stored configuration options and changes will be applied immediately.
Important: Because configuration options are applied partially, it’s not possible to reset option to default value just by omitting it’s key name and calling configure method. To reset configuration option to the default value, it’s key must be set to null!
configure
null
// Example: reset postTemplate to default BackgroundGeolocation.configure({ postTemplate: null });
Platform: iOS, Android
Get current configuration. Method will return all configuration options and their values in success callback. Because configure method can be called with subset of the configuration options only, getConfig method can be used to check the actual applied configuration.
getConfig
BackgroundGeolocation.getConfig(function(config) { console.log(config); });
Start background geolocation.
Stop background geolocation.
One time location check to get current location of the device.
timeout
maximumAge
enableHighAccuracy
location
code
message
Error codes:
Deprecated: This method is deprecated and will be removed in next major version. Use checkStatus as replacement.
checkStatus
One time check for status of location services. In case of error, fail callback will be executed.
enabled
Check status of the service
isRunning
locationServicesEnabled
authorization
Authorization statuses:
Note: In the Android concept of authorization, these represent application permissions.
Platform: Android >= 6, iOS >= 8.0
Show app settings to allow change of app location permissions.
Platform: Android
Show system settings to allow configuration of current location sources.
Method will return all stored locations. This method is useful for initial rendering of user location on a map just after application launch.
locations
Array
BackgroundGeolocation.getLocations( function (locations) { console.log(locations); } );
Method will return locations which have not yet been posted to server.
Method will return locations which have not yet been posted to server and delete to avoid getting them again.
Delete location with locationId.
Note: You don’t need to delete all locations. The plugin manages the number of stored locations automatically and the total count never exceeds the number as defined by option.maxLocations.
option.maxLocations
Delete all stored locations.
Note: Locations are not actually deleted from database to avoid gaps in locationId numbering. Instead locations are marked as deleted. Locations marked as deleted will not appear in output of BackgroundGeolocation.getValidLocations.
BackgroundGeolocation.getValidLocations
Platform: iOS
Normally the plugin will handle switching between BACKGROUND and FOREGROUND mode itself. Calling switchMode you can override plugin behavior and force it to switch into other mode.
In FOREGROUND mode the plugin uses iOS local manager to receive locations and behavior is affected by option.desiredAccuracy and option.distanceFilter.
option.desiredAccuracy
option.distanceFilter
In BACKGROUND mode plugin uses significant changes and region monitoring to receive locations and uses option.stationaryRadius only.
option.stationaryRadius
// switch to FOREGROUND mode BackgroundGeolocation.switchMode(BackgroundGeolocation.FOREGROUND_MODE); // switch to BACKGROUND mode BackgroundGeolocation.switchMode(BackgroundGeolocation.BACKGROUND_MODE);
Platform: Android, iOS
Force sync of pending locations. Option syncThreshold will be ignored and all pending locations will be immediately posted to syncUrl in single batch.
Return all logged events. Useful for plugin debugging.
limit
fromId
minLevel
Function
*Example of infinite log scrolling
Format of log entry:
id
timestamp
level
stackTrace
Unregister all event listeners for given event. If parameter event is not provided then all event listeners will be removed.
event
Location
stationary
activity
Activity
error
{ code, message }
status
start
stop
foreground
background
abort_requested
http_authorization
provider
time
latitude
longitude
accuracy
speed
altitude
bearing
isFromMockProvider
mockLocationsEnabled
Locations parameters isFromMockProvider and mockLocationsEnabled are not posted to url or syncUrl by default. Both can be requested via option postTemplate.
Note: Do not use location id as unique key in your database as ids will be reused when option.maxLocations is reached.
There are repeatedly reported issues with some android devices not working in the background. Check if your device model is on dontkillmyapp list before you report a new issue. For more information check out dontkillmyapp.com.
Another confusing fact about Android services is the concept of foreground services. Foreground service in the context of Android OS is a different thing than background geolocation service of this plugin (they’re related thought). The plugin’s background geolocation service actually becomes foreground service when app is in the background. Confusing, right? :D
If service wants to continue to run in the background, it must “promote” itself to foreground service. Foreground services must have visible notifications, which is the reason why you can’t disable drawer notifications.
foreground service
The notification can only be disabled, when the app is running in the foreground, by setting the config option startForeground: false (this is the default option), but will always be visible in the background (if the service was started).
startForeground: false
Recommended reading: https://developer.android.com/about/versions/oreo/background