capacitor-community / background-geolocation

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

AddWatcher only retrieves location once #100

Closed GonzaloCorchon closed 1 year ago

GonzaloCorchon commented 1 year ago

Hi,

I'm building an Ionic 7 app (Angular) for Android. I'm testing on a POCO X3 PRO with Android 12. For some reason, after adding a watcher the plugin only calls once the callback, even with the app in foreground mode. The GPS notification icon is present on the toolbar all the time and I don't remove the watcher until my component is destroyed. Using an interval timer I can call the addWatcher/removeWatcher and get the current location every few seconds, but I think this is not the way it's meant to be. Using an interval timer also stops retrieving location after a few minutes when the app is in background mode.

I have also set the application location permission to be always allowed.

My code is basically the same as the one exposed in the README.md.

BackgroundGeolocation.addWatcher({ 
  backgroundMessage: "Cancel to prevent battery drain.",
  backgroundTitle: "Tracking You.",
  requestPermissions: true,
  stale: false,
  distanceFilter: 50 }, 
  (position?:Location, error?:CallbackError) => {
    if(error?.code === "NOT_AUTHORIZED") {
      if(confirm("GPS permission required for a full experience")){
        BackgroundGeolocation.openSettings();
        return;
      }
    }
    console.log(position);
}).then( callbackId => {
  this.callbackId = callbackId;
});

Currently I have this permissions in the AndroidManifest.xml file

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-feature android:name="android.hardware.location.gps" />

Is there somehting I am missing?

Thank you!

diachedelic commented 1 year ago

I see you have the distance filter set to 50 metres. You will only receive a new location after travelling 50 metres from the previous one. Does that answer your question?

GonzaloCorchon commented 1 year ago

Yes my friend, I have just gone for a long walk and it works perfectly. It was my fault.

diachedelic commented 1 year ago

OK, good stuff.