Background location works only for 5 minute in ionic vue

I am using plugin

to watch the user’s location. then I am tracking that location with the help of a pusher. this background location works only for 5 minutes after that it just stops. I am using Capacitor’s Background task. But that plugin also keeps data on phone after the user opens the app. the Background task sends data to the pusher.

Here is watch location function

  getLocation: async function () {
      BackgroundGeolocation.initialize({
        notificationText: "Your app is running, tap to open.",
        notificationTitle: "App Running",
        updateInterval: 10000,
        requestedAccuracy: BgGeolocationAccuracy.HIGH_ACCURACY,
        // Small icon has to be in 'drawable' resources of your app
        // if you does not provide one (or it is not found) a fallback icon will be used.
        smallIcon: "ic_small_icon",
        // Start getting location updates right away. You can set this to false or not set at all (se below).
        startImmediately: true,
      });
      // const geolocation = new Geolocation.Geolocation();
      BackgroundGeolocation.addListener("onLocation", (location) => {
        // console.log("Got new location", location);
        this.subscribe(location.longitude, location.latitude);
        console.log(location)
      });
    
      BackgroundGeolocation.addListener("onPermissions", (location) => {
        // console.log("BGLocation permissions:", location);
        this.subscribe(location.longitude, location.latitude);
        
        // Do something with data
      });
      BackgroundGeolocation.start();
    },

Then calling function in mounted()

mounted(){
this.getLocation
  App.addListener("appStateChange", (state) => {
       setInterval(this.getLocation, 120000);
      if (!state.isActive) {
        BackgroundTask.beforeExit(async () => {
          setInterval(this.getLocation, 120000);
          console.og('Why')
        });
      }
      if (state.isActive) {
        setInterval(this.getLocation, 120000);
        console.log('Active')
      }
    });
}

No need to tag in a different thread when your post is just as new…

I dont know about that plugin, haven’t tried it out. Check out the official community plugin here though and try that.

I tried it but giving error on registerPlugin() . Not finding register plugin

It’s working fine here, take a look.

1 Like

I think i found issue Here is what google doc says

In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently an app can retrieve the user’s current location while the app is running in the background. Under these conditions, apps can receive location updates only a few times each hour.

Its means that /i can not get user’s location more than few times. For that I need to user location service? whats that?

Well as android 8+ not allow continue get user’s location so we need to create foreground services . it works for me