transistorsoft/nativescript-background-geolocation-lt

Dynamically enabling/disabling the HTTP feature

neil-119 opened this issue · 2 comments

I need to enable the HTTP feature and add an auth header when the user is logged in, but disable that feature when the user isn't logged in. But it seems that the ready() method expects these on init, and setConfig() doesn't take effect until the app is restarted. So how can I enable http and later disable it at runtime? Thanks!

iOS / Android version: iOS 11.4.1, Android 8.1, Android 9.0
NativeScript version: 4.2.0 (incl. latest runtimes)
Plugin version: latest

#ready only consumes the supplied config at first install of your app.

You should always #ready the plugin, regardless of logged-in / not.

export class HelloWorldModel extends Observable {

  constructor() {
    BackgroundGeolocation.on('location', (location) => { ... });

    BackgroundGeolocation.ready({
      url: '...',      
    }, (state) => {
      console.log('[ready] BackgroundGEolocation');
      // NOTE:  we do NOT start tracking until we know user is logged in.
    });
  }
  
  onLogin() {
    BackgroundGeolocation.setConfig({
      headers: {"X-FOO", "bar"}
    }, (state) => {
      // Now that user is logged in, start tracking them.
      BackgroundGeolocation.start();
    });
  }

  onLogout() {
    // Presumably you no longer want to track users when they're logged out.  Just turn off the plugin.
    BackgroundGeolocation.stop();
  }
}

Super, thanks.