But what if you need to implement some custom business logic in the "headless" state, such as posting a local notification or executing #getCurrentPosition in a heartbeat event?
The BackgroundGeolocationplugin for React Native and Cordova now provides a mechanism for you to implement your own custom code to handle all the plugin's events while in the "Headless" state (heartbeat, location, motionchange, geofence, etc).
Try it out
With your React Native debugger connected, terminate your app and observe log messages arriving from your HeadlessTask.
First, create a Java file named BackgroundGeolocationHeadlessTask.java in your src folder (eg: src/android/BackgroundGeolocationHeadlessTask.java). This file must extend HeadlessTask and implement HeadlessTask.Receiverexactly as shown below:
Next, you need to instruct Cordova to copy your custom source-file file into the background-geolocation plugin's src-tree. In your Cordova config.xml, add the following element to copy your custom source-file:
Finally, configure the plugin with enableHeadless: true:
Try it out
While observing $ adb logcat, terminate your app and observe log messages arriving from your custom BackgroundGeolocationHeadlessTask. With just a small handful of Java code, you're now receiving all events emitted by the plugin while in the Headless state with your app terminated.
A great way to field-test the background-geolocation plugin is to use our test-server which we provide open-source at Github background-geolocation-console. It's a simple node-server and web-application for quickly viewing your device tracking. It's very easy to setup and configure the background-geolocation plugin to post to it.
The Background Geolocation sample-apps (Cordova, React Native) are already pre-configured to post to a hosted version of the tracking console at http://tracker.transistorsoft.com.
The background geolocation console is particularly useful for geofence-testing. Geofence transitions are marked by coloured dot on the geofence edge (red = exit, green = entry, yellow=dwell) with a black polyline pointing to the triggering location.
If you want to post to the tracking console in your own app, it's very easy: simply provide some device information in the plugin's #params config:
Yes, in spite of Android Oreo's strict new Background Execution Limits, the Background Geolocation plugin fully supports Android 8. The caveat however, is that the foregroundService: true option is now enforced.
Here's a nice little presentation by Harry Tormey at React Chicago which clearly lays out the complexities involved with tracking a device's location in the background and why this needs to be done directly with Native APIs.
This is exactly what the Background Geolocation plugin does for you.
BackgroundGeolocation now supports infinite geofencing. Why is this important? Because the native platforms limit the number of geofences which can be simultaneously monitored. For iOS the limit is 20 while Android allows 100.
Previously, you had to manually manage which geofences to monitor based upon the current location, perhaps asking the server periodically which geofences were within range.
Now you can load any number of geofences into the plugin (tested with tens-of-thousands). The plugin will periodically perform a geospatial query upon the geofences in the database, activating those closest within a configurable geofenceProximityRadius.
Following is an animation of this feature performed in the iOS simulator, where a series of geofences have been loaded along the entire City Drive route.
With Infinite Geofencing, BackgroundGeolocation is the most advanced geofencing plugin available, more advanced than any dedicated geofencing plugin.
During June 12-27 2016, I took a vacation in Iceland, circumnavigating the entire island. This was a great opportunity to do a long-term field-test in a remote, mountainous and sparsely populated territory.
I used two phones: iPhone 6s @ 9.3.2 and Android Nexus 5 @ 6.0.1 running Cordova Background Geolocation 1.7.0.
The results were much better than expected, even while travelling up rugged mountain roads and remote gravel-roads.