capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
194 stars 57 forks source link

background location is canceled #117

Open GerardoTovar opened 5 months ago

GerardoTovar commented 5 months ago

Describe the bug When I start the watch code on iOS everything works correctly but on Android When it runs to start in the background the location icon appears and then disappears

    this.plt.pause.subscribe( () => {// background
      // dont work on android
      this.bgGeoS.enable_bg_track()
    });

    this.plt.resume.subscribe(() => {// foreground
      this.bgGeoS.disable_bg_track()
    });

But if before sending the app to the background it already has the watch code initialized. works correctly Maybe the way I want to abort this functionality is not correct. I tried to find out if there was a way to detect more before the platform goes into the background

To Reproduce Just create a project and initialize the watch code just when it goes to the background

Expected behavior initializing the watch code just when the platform goes to the background should work on android

Smartphone (please complete the following information):

Additional context

  async enable_bg_track() {
    const callback = (position?: Location, error?: CallbackError) =>{
      this.last_location = position || undefined
      console.log(this.last_location);
      if(error) {
        console.error(error);
        if(this.idWatch) BackgroundGeolocation.removeWatcher({id:this.idWatch});
      }
    }
    const options: WatcherOptions = {
      requestPermissions: false, 
      stale: false,
      backgroundTitle: "Obteniendo tu ubicación",
      backgroundMessage: "Cancelar"
    }
    BackgroundGeolocation.addWatcher(options, callback).then((id)=> { this.idWatch = id }).catch(err =>{ console.error(err) });
  }

At the moment my solution is to activate tracking when I start the application. If anyone has any suggestions I would like to hear them.

diachedelic commented 5 months ago

I think believe this is a limitation of foreground services on Android. A foreground service can not usually be started from the background. https://developer.android.com/develop/background-work/services/foreground-services#background-start-restrictions

Please let me know if you find a solution.

diachedelic commented 5 months ago

Try inspecting the error log for this message: https://github.com/capacitor-community/background-geolocation/blob/fe780a4c8672f7b526792c2815d6e99652a55cd7/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocationService.java#L174

diachedelic commented 5 months ago

Somewhat related to https://github.com/capacitor-community/background-geolocation/issues/86.