Location is not being sent when app is closed. Can anyone point out what i am missing?


#1

I am working on an Ionic app. I am trying to send location in background every 1 hour. I am using official Background Geolocation plugin. Location is being sent when the app is open either in foreground or background but as soon as i close the app my location is not being sent to database. Although my debug mode is active and i can see toast message and beep sound even after closing app. Can anyone point out where i am making mistake? Here is my code.

{
platform.ready().then(() => {
this.getLocation();
this.backgroundGeolocation.start();
});
}

getLocation() {
const config: BackgroundGeolocationConfig = {
  locationProvider: this.backgroundGeolocation.LocationProvider.ANDROID_DISTANCE_FILTER_PROVIDER,
  desiredAccuracy: 10,
  stationaryRadius: 5,
  startOnBoot: true,
  notificationIconColor: '#4CAF50',
  interval: 5000,
  fastestInterval: 5000,
  activitiesInterval: 5000,
  distanceFilter: 5,

  debug: true, //  enable this hear sounds for background-geolocation life-cycle.
  stopOnTerminate: false, // enable this to clear background location settings when the app terminates
  stopOnStillActivity: false
};


this.backgroundGeolocation.configure(config).subscribe((location) => {
  this.loc.push(location);
  this.storage.save('Location', this.loc).subscribe();
  console.log('Location :', location);
  this.sendLocation(location);
});

}

sendLocation(data) {
const url = 'my-url';
const payload = {
  date_added: moment(data.time).format('DD/MM/YYYY'),
  identity: '',
  ipaddress: '',
  latitude: data.latitude,
  longitude: data.longitude,
  mobile: 'phoneNo',
  network_name: '',
  status: '',
  time_added: moment(data.time).format('hh:mm A'),
}
this.http.post(url, payload).map(res => res.json()).subscribe(data => console.log('location inserted: ', data))

}


#2

Please edit your post, it is not very readable at the moment.
Use the </> button above the input field to format your code, command line output or error message (select the text first, then click the button or wrap it in ``` manually). Check the preview if it looks better. This will make sure your text is readable and if it recognizes the programming language it also automatically adds code syntax highlighting. Thanks.


#3

The Background Geolocation Plugin only works when your is running in foreground or background. It doesn’t work when the app is closed.

Sending the user’s Geolocation every x minutes (even when the app is closed) won’t work.
You could take a look at geofences, maybe that already helps to meet your requirements. Geofences will be checked even when the app is closed.