react-native-background-geolocation: HeadLessTask never receives location or geofence events.
Plugin version: 3.0.7 Platform: iOS or Android: Android OS version: MAC OS Mojave (10.14.5) Device manufacturer / model: Any React Native version (react-native -v): 0.49.9 Plugin config
Here is how my index.js looks like:
import React, {Component} from 'react';
import {
AppRegistry
} from 'react-native';
import BackgroundGeolocation from 'react-native-background-geolocation';
var AppContainer = require('./src/AppContainer/AppContainer');
class SafeApp extends Component {
constructor(props) {
super(props);
};
render() {
return (<AppContainer BackgroundGeolocation={BackgroundGeolocation}/>);
}
}
AppRegistry.registerComponent('SafeApp', () => SafeApp);
let HeadlessTask = async (event) => {
let params = event.params;
console.log('[BackgroundGeolocation HeadlessTask] -', event.name, params);
//let loc = await getCurrentPosition(); COMMENTED
let geofence = event;
switch(event.name) {
case "location":
console.log('RECIEVED LOCATION');
break;
case "geofence":
console.log('RECIEVED GEOFENCE');
if (geofence.action.toLocaleLowerCase() == "enter") {
}
if (geofence.action.toLocaleLowerCase() == "exit") {
}
break;
}
}
let getCurrentPosition = () => {
return new Promise((resolve) => {
resolve(BackgroundGeolocation.getCurrentPosition({timeout: 30}));
});
};
BackgroundGeolocation.registerHeadlessTask(HeadlessTask);
Problem :
Headless task runs and can be seen in debugger mode, but only few events like providerChange, connectivityChange and then terminate fires one time.

How to get location and geofence enter/exit events?
When I uncomment
//let loc = await getCurrentPosition(); COMMENTED
line which calls an async method to get currentPosition then task starts receiving ‘location’ event. I know this is not a proper way to do this.
But, for geofences I do not know any such hack even.
Could someone please provide a proper way to use HeadLessTask. Thanks in advance.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 22 (10 by maintainers)
If you want to cause a “location” event to fire, walk around with the device or simply shake the device vigorously for about 30s to simulate walking.