capacitor-community / background-geolocation

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

Android notification stays after calling removeWatcher #4

Closed pdrhlik closed 3 years ago

pdrhlik commented 3 years ago

I start by calling addWatcher() and then after some time I decide to remove it by calling removeWatcher(). My understanding would be that the sticky notification would disappear when there is no active watcher. No watcher means no location tracking and that should be no notification, is that right?

I have a special option inside my app settings that allows users to switch background geolocation on/off. The idea was to remove the watcher when app is being backgrounded and then start it again when it's foregrounded.

The sample approach and logic I tried is something like this. I'm using Capacitor's App plugin to detect if the app was backgrounded or foregrounded.

App.addListener("appStateChange", state => {
  if (!this.canUseBackgroundGps) {
    if (state.isActive) {
      BackgroundGeolocation.addWatcher(/* ... */);
    } else {
      BackgroundGeolocation.removeWatcher(/* ... */);
    }
  }
});

I only tested and observed this behaviour on Android. I'll test if it behaves the same on iOS tomorrow.

Thanks for any heads up, Patrik

diachedelic commented 3 years ago

The notification certainly should disappear – are you absolutely sure that all watchers have been removed at that point? Do you retain the id from addWatcher for later use with removeWatcher?

On 1 Feb 2021, at 6:38 am, Patrik Drhlik notifications@github.com wrote:

I start by calling addWatcher() and then after some time I decide to remove it by calling removeWatcher(). My understanding would be that the sticky notification would disappear when there is no active watcher. No watcher means no location tracking and that should be no notification, is that right?

I have a special option inside my app settings that allows users to switch background geolocation on/off. The idea was to remove the watcher when app is being backgrounded and then start it again when it's foregrounded.

The sample approach and logic I tried is something like this. I'm using Capacitor's App plugin to detect if the app was backgrounded or foregrounded.

App.addListener("appStateChange", state => { if (!this.canUseBackgroundGps) { if (state.isActive) { BackgroundGeolocation.addWatcher(/ ... /); } else { BackgroundGeolocation.removeWatcher(/ ... /); } } }); I only tested and observed this behaviour on Android. I'll test if it behaves the same on iOS tomorrow.

Thanks for any heads up, Patrik

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/capacitor-community/background-geolocation/issues/4, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACLZQGWZUHV7ZUEACFW56TS4XBETANCNFSM4W3W5GZA.

diachedelic commented 3 years ago

Note that on iOS, there is no notification.

pdrhlik commented 3 years ago

The notification certainly should disappear – are you absolutely sure that all watchers have been removed at that point? Do you retain the id from addWatcher for later use with removeWatcher?

The problem was that I thought addWatcher() was returning the watcher ID as a Promise. It works as expected in 0.3.8 anticipating a string instead.

Thanks.

sander-spruit commented 3 years ago

I am puzzled by this same issue. I'm calling

this.watchId = BackgroundGeolocation.addWatcher({
/* options, callback */
});

and later

BackgroundGeolocation.removeWatcher({ id: this.watchId });

But the notification sticks? Is that not the right approach?

diachedelic commented 3 years ago

The addWatcher function does not return an ID, it returns a Promise which resolves to an ID.

matudelatower commented 2 years ago

Any suggest