On Android, when you terminate your app with the BackgroundGeolocation plugin configured with
url, its Android background Service will continue posting locations to your server.
But what if you need to implement some custom business logic in the “headless” state, such as posting a local notification or executing
getCurrentPositionin a heartbeat event?
The BackgroundGeolocation plugin 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.
For more information, see the React Native Background Geolocation Wiki Android Headless Mode.
Cordova / Ionic
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.Receiver exactly 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
Try it out
$ 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.
For more information, see the Cordova Background Geolocation Wiki Android Headless Mode.