The BackgroundGeolocation SDK hosts its own flexible and robust native HTTP & SQLite persistence services. To enable the HTTP service, simply configure the SDK with an Config.url:
The SDK immediately inserts each recorded location into its SQLite database. This database is designed to act as a temporary buffer for the HTTP service and the SDK strongly desires an empty database. The only way that locations are destroyed from the database are:
Successful HTTP response from your server (200, 201, 204).
The SDK's HTTP service operates by selecting records from the database, locking them to prevent duplicate requests then uploading to your server.
By default, the HTTP Service will select a single record (oldest first; see locationsOrderDirection) and execute an HTTP request to your Config.url.
Each HTTP request is synchronous — the HTTP service will await the response from your server before selecting and uploading another record.
If your server returns an error or doesn't respond, the HTTP Service will immediately halt.
Configuring batchSynctrue instructs the HTTP Service to select all records in the database and upload them to your server in a single HTTP request.
Use maxBatchSize to limit the number of records selected for each batchSync request. The HTTP service will execute synchronous HTTP batch requests until the database is empty.
The SDK's HTTP Service will upload recorded locations as JSON to your Config.url (See Location for the JSON schema) with Content-Type application/json. The data can be found by your server in the "HTTP request body".
If your server does not return a 20x response (eg: 200, 201, 204), the SDK will UNLOCK that record. Another attempt to upload will be made in the future (until maxDaysToPersist) when:
By default, the SDK is configured for autoSync:true and will attempt to immediately upload each recorded location to your configured Config.url.
Use autoSyncThreshold to throttle HTTP requests. This will instruct the SDK to accumulate that number of records in the database before calling upon the HTTP Service. This is a good way to conserve battery, since HTTP requests consume more energy/second than the GPS.
Location record inserted into SDK's SQLite database.
3
✅Locked
SDK's HTTP service locks a record (to prevent duplicate HTTP uploads).
4
🔵HTTP POST
SDK's HTTP service attempts an HTTP request to your configured url.
5
🔵Response
Response from your server.
6
✅DESTROY|UNLOCK
After your server returns a 20x response, the SDK deletes that record from the database. Otherwise, the SDK will UNLOCK that record and try again in the future.
The SDK has a "Remote Procedure Call" (RPC) mechanism, allowing you to invoke commands upon the SDK's API by returing a JSON response from the server containing the key "background_geolocation": [...].
Within the returned [...], you may return one or more commands to invoke upon the SDK. Each command takes the form of an [], with a required first element String command, along with an optional
second element Argument:string|boolean|number|Object depending upon the context of the command.
The event-object provided to BackgroundGeolocation.onHttp when an HTTP response arrives from your configured Config.url.
HTTP Guide
The BackgroundGeolocation SDK hosts its own flexible and robust native HTTP & SQLite persistence services. To enable the HTTP service, simply configure the SDK with an Config.url:
The SQLite Database
The SDK immediately inserts each recorded location into its SQLite database. This database is designed to act as a temporary buffer for the HTTP service and the SDK strongly desires an empty database. The only way that locations are destroyed from the database are:
200,201,204).The HTTP Service
The SDK's HTTP service operates by selecting records from the database, locking them to prevent duplicate requests then uploading to your server.
trueinstructs the HTTP Service to select all records in the database and upload them to your server in a single HTTP request.Capturing the data at your server
The SDK's HTTP Service will upload recorded locations as JSON to your Config.url (See Location for the JSON schema) with
Content-Type application/json. The data can be found by your server in the "HTTP request body".PHP
Node with
expressRails
HTTP Failures
If your server does not return a
20xresponse (eg:200,201,204), the SDK willUNLOCKthat record. Another attempt to upload will be made in the future (until maxDaysToPersist) when:pause/resumeevents.fetchevents.Receiving the HTTP Response.
You can capture the HTTP response from your server by listening to the onHttp event.
autoSync: trueBy default, the SDK is configured for autoSync:true and will attempt to immediately upload each recorded location to your configured Config.url.
Manual Invoking Upload
The SDK's HTTP Service can be summoned into action at any time via the method BackgroundGeolocation.sync.
Config.params, headers and extras
JSONdata of each HTTP request.Custom
JSONSchema: locationTemplate and geofenceTemplateThe default HTTP
JSONschema for both Location and Geofence can be overridden by the configuration options locationTemplate and geofenceTemplate, allowing you to create any schema you wish.Disabling HTTP requests on Cellular connections
If you're concerned with Cellular data-usage, you can configure the plugin's HTTP Service to upload only when connected to Wifi:
HTTP Logging
You can observe the plugin performing HTTP requests in the logs for both iOS and Android (See Wiki Debugging):
📍Location✅INSERT✅Locked🔵HTTP POSTurl.🔵Response✅DESTROY|UNLOCK20xresponse, the SDK deletes that record from the database. Otherwise, the SDK willUNLOCKthat record and try again in the future.Controlling the SDK with HTTP Responses (RPC)
The SDK has a "Remote Procedure Call" (RPC) mechanism, allowing you to invoke commands upon the SDK's API by returing a JSON response from the server containing the key
"background_geolocation": [...].Within the returned
[...], you may return one or more commands to invoke upon the SDK. Each command takes the form of an[], with a required first elementString command, along with an optional second elementArgument:string|boolean|number|Objectdepending upon the context of thecommand.The SDK will run each of these commands synchronously upon itself.
Supported RPC Commands
startBackgroundGeolocation.start()stopBackgroundGeolocation.stop()startGeofencesBackgroundGeolocation.startGeofences()changePaceBooleanBackgroundGeolocation.changePace(argument)setConfig{Config}BackgroundGeolocation.setConfig(argument)addGeofence{Geofence}BackgroundGeolocation.addGeofence(argument)addGeofences[{Geofence}, ...]BackgroundGeolocation.addGeofences(argument)removeGeofenceidentifier:StringBackgroundGeolocation.removeGeofence(argument)removeGeofences[identifier:String,...]BackgroundGeolocation.removeGeofences(argument)If provided no argument, remove all; otherwise remove provided list of identifiersuploadLogurl:StringdestroyLogBackgroundGeolocation.destroyLogSimple Example:
#stopYour server could return a response telling the SDK to BackgroundGeolocation.stop:
When returning just a single command, you can optionally omit the root
[ ]:Arguments
The 2nd param to each action is optional but depends upon the context of the command. For example,
#changePacerequires abooleanargument:Object Arguments
Some commands receive an
{ }argument, like#setConfig:Multiple Actions
You could tell the plugin to both
#startand#changePace: