Request background geolocation and check response


#1

I’m working on the onboarding process for my app, which requires background geolocation. It can’t work at all without location permission, so during the onboarding I need to make sure that the permission is granted.

I have a location provider, which handles location tracking for the whole app, however backgroundGeolocation.start() always resolves, even when location permission is not granted. At the moment, my code looks like this:

configureBackground() {
  let opts =  {
    desiredAccuracy: 10,
    stationaryRadius: 10,
    distanceFilter: 10,
    pauseLocationUpdates: false,
    stopOnTerminate: false
  }

  this.backgroundGeolocation.configure(opts).subscribe(position => {
    this.ref = {
      lat: position.latitude,
      lng: position.longitude
    }

    this.pos.next(this.ref)

    // confirm state updated
    this.backgroundGeolocation.finish();
  })
}

startTracking() {
  this.configureBackground();
  return this.backgroundGeolocation.start();
}

I use it as follows:

locationService.startTracking()
  .then(() => {
    // Go to next component of onboarding page
  })
  .catch(err => {
    // User didn't grant location, show error page
  })

.catch is never called, even if location permission is denied.

How can I start tracking and go to an error page if permission is denied?


#2

EDIT: I take it back, this doesn’t fix my issue
I resolved this using the Diagnostic plugin, which has a method requestLocationAuthorization(mode)

Perhaps a link should be added to the geolocation docs regarding this, as it seems to be the only way to request permission without actually starting location tracking.


#3

I’ve tried using the Diagnostic plugin, however it has the same issue.

requestLocationAuthorization(mode) returns a promise which resolves immediately with the current permission status. It does not wait for the user to accept/deny the request, which makes it completely useless.

It seems most Ionic Native plugins are completely broken, with useless documentation…


#4

This appears to be an issue with the cordova plugin, I’ve provided more infomation here: https://github.com/dpa99c/cordova-diagnostic-plugin/issues/185


#5

This issue is now fixed in cordova-diagnostic-plugin@3.6.3