Ionic-native: Background Geolocation documentation missing /ngx

Created on 30 Mar 2020  路  13Comments  路  Source: ionic-team/ionic-native

As for my previous post on the ionic forum: https://forum.ionicframework.com/t/background-geolocation-documentation-missing-ngx/186030

Background geolocation plugin
https://ionicframework.com/docs/native/background-geolocation

example misses

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationEvents, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';

/ngx

and causing a lot of issues on mauron85 github too.

Most helpful comment

Hi Tobika,

It's a pleasure to answer to the community question, even if this is the repo of Ionic native and it would be appropriate to discuss this topic at least on the moran85 repo to involve properly who's directly interested and has a long story and strong experience in the development of that advanced system.

About the permission issue you queried for, it's a common security to try to prevent valuable user data to get out of the control and to show the system permission modal with the detailed information about the usage of the provider asked for, and so in the latest versions, the worldwide phone os retailers of enterprise size has tried to pushed on a new way of finding the agreement from the user without giving the possibility to check for the user location so often as it was originarly. The permission system is slightly different but the mechanism is quite the same, and yes, it would be fine to run the geolocation plugin on those versions, the author's still actively maintaining it and there are many software developer in the user community around it.

About your second question, it is surely possible that a pay plugin with one or some active developers that works dedicately on it could fit at best your project requirements, especially if you prefer to get a ready made solution and apply to your project without many configurations or customizations, or if you're interested in their specific feature set like to get your route for car navigations and so on.

Side note: do not let your app go outdated, always take the time with your team to get the latest version of the ionic native plugin and upgrade your plugins inside your project periodically by checking the current version from this page:
https://github.com/ionic-team/ionic-native/releases
this is a best procedure in development

Cheers, Jackie

All 13 comments

Integration:

as for v 3.0 of cordova plugin

  • iOS finish method replaced with startTask and endTask
  BackgroundGeolocation.on('location', function(location) {
    // handle your locations here
    // to perform long running operation on iOS
    // you need to create background task
    BackgroundGeolocation.startTask(function(taskKey) {
      // execute long running task
      // eg. ajax post location
      // IMPORTANT: task has to be ended by endTask
      BackgroundGeolocation.endTask(taskKey);
    });
  });

in our case would become:

    this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
      console.log(location);

       // handle your locations here
       // to perform long running operation on iOS
       // you need to create background task
        this.backgroundGeolocation.startTask().then((taskKey) => {
          // execute long running task
          // eg. ajax post location
          // IMPORTANT: task has to be ended by endTask
          this.backgroundGeolocation.endTask(taskKey);
        });
    });

I just stumbled on the same problem. Guess it's safer to use the plugin without ionic-native for the moment.

I just stumbled on the same problem. Guess it's safer to use the plugin without ionic-native for the moment.

Actually I did used the native version as follow:

npm install:

ionic cordova plugin add @ionic-native/[email protected]
ionic cordova plugin add @mauron85/[email protected]

=> package.json

ionic plugin install @ionic-native/background-geolocation": "^5.23.0",
"@mauron85/cordova-plugin-background-geolocation": "^3.1.0",

In my implementation:

import {
  BackgroundGeolocation,
  BackgroundGeolocationConfig,
  BackgroundGeolocationEvents,
  BackgroundGeolocationLocationProvider,
  BackgroundGeolocationLogLevel,
  BackgroundGeolocationResponse
} from '@ionic-native/background-geolocation/ngx';

constructor(
      private backgroundGeolocation: BackgroundGeolocation
  ) {}

// method:

const subscription = this.backgroundGeolocation.on(BackgroundGeolocationEvents.location)
      .subscribe((location: BackgroundGeolocationResponse) => {

        this.backgroundGeolocation.startTask().then((taskKey) => {

          console.log('geolocation received:', location);

          this.backgroundGeolocation.endTask(taskKey);
        });

      }, error => {
          console.log('geolocation error:', error);
      });

    this.backgroundListener.add(subscription);

});

But let me know if you would have any issue with the version or step proposed in the package json installation.

Cheers, Jackie

@jackie-d thanks for this info, so actually endTask() exists :+1: (didn't check that far) and It's just the documentation that needs updating, and maybe to be clear about it finish() should be removed from the typings.

Yes, as best practice I would recommend to manage to disable the typings manually otherwise you will get continuous errors on compilation or live watch even if you developed it perfectly and it's working at its best.

@jackie-d thanks for all your feedback, as we are the only ones in this issue I might shortly hijack it to ask you another question about the plugin ;-)
please ignore it if you don't have time to answer

Do you still use the plugin successfully with android 11 & 10 and iOS 14?
It seems to me we can't choose the permission to have geoloc in background anymore? Didn't find an issue for this problem though. But I don't think I have background geoloc on android 11 with simulator.
UPDATE: I think android 10 is fine now, I actually forgot the relevant permission :/
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Master question: Did you get the premium plugin from https://github.com/transistorsoft/cordova-background-geolocation-lt
It seems so well maintained and documented that the money should be easily worth it :-)

Hi Tobika,

It's a pleasure to answer to the community question, even if this is the repo of Ionic native and it would be appropriate to discuss this topic at least on the moran85 repo to involve properly who's directly interested and has a long story and strong experience in the development of that advanced system.

About the permission issue you queried for, it's a common security to try to prevent valuable user data to get out of the control and to show the system permission modal with the detailed information about the usage of the provider asked for, and so in the latest versions, the worldwide phone os retailers of enterprise size has tried to pushed on a new way of finding the agreement from the user without giving the possibility to check for the user location so often as it was originarly. The permission system is slightly different but the mechanism is quite the same, and yes, it would be fine to run the geolocation plugin on those versions, the author's still actively maintaining it and there are many software developer in the user community around it.

About your second question, it is surely possible that a pay plugin with one or some active developers that works dedicately on it could fit at best your project requirements, especially if you prefer to get a ready made solution and apply to your project without many configurations or customizations, or if you're interested in their specific feature set like to get your route for car navigations and so on.

Side note: do not let your app go outdated, always take the time with your team to get the latest version of the ionic native plugin and upgrade your plugins inside your project periodically by checking the current version from this page:
https://github.com/ionic-team/ionic-native/releases
this is a best procedure in development

Cheers, Jackie

Thank @jackie-d for the solution.

Quick question, what would be the benefit of using the plugin over a combination of the Background task & Geolocation from Capacitor.

I really trying to keep my app away from cordova plugins, so I'm doing my research.

Note: Sorry if this is not the appropriate medium to ask for advice

@jongbonga if I understand the documentation of background task correctly you can only do one last task before the app is closed. It won't stay alive to continue to collect geolocation data.

The plugin allows you to continue to collect locations even if your app is closed, but only via an http POST that is not in the context of your application. (so no javascript before sending the location) This procedure is not shown in the above solution. In this one once your app stops you stop getting locations.

Another not battery friendly way (and not sure that the OS won't stop the app) would be to try to keep the app open and use this plugin or just make location requests via an interval in your app.

Thanks @tobika for the reply.

This is where I get confused... both the capacitor & Cordova plugins have the endTask / finish method... which means they can run indefinitely until the method is called.

But there's not practical example of how it is done.

Let say I want to track a driver location, do I end the task every time the plugin return 1 set coordinates then do it over and over again until my login end the trip...? Or.... that 1 task will be sending the data over and over until it's end trip time?

I'm struggling to implement them properly

@jongbonga there is no guarantee that this method can run indefinitely.

This method should finish in less than 3 minutes or your app risks being terminated by the OS.

      // Must call in order to end our task otherwise
      // we risk our app being terminated, and possibly
      // being labeled as impacting battery life
      BackgroundTask.finish({
        taskId
      });

And this function only runs once when the app is put into background, only when the user revives the app and closes it again will the function be run.

Thank @jackie-d for the solution.

Quick question, what would be the benefit of using the plugin over a combination of the Background task & Geolocation from Capacitor.

I really trying to keep my app away from cordova plugins, so I'm doing my research.

Note: Sorry if this is not the appropriate medium to ask for advice

Yes that could be a solution, and also keep the reference to software author when you post question about their product because in this case he could really be helpful on updating you with every latest feature and support notes, as he's doing a very good job in searching for keeping it updated.

Have a good day.

About the other questions you made, as I already said, please refer to the mauron85 repo for specific question about implementation and eventual support. Just for completeness, it's good to specify that yes, it's possible to have the background task to be repeat every 30s/2m depending on the phone model and phone battery lifecycle state and to make the code run inside the task to handle the latest data collection and additional operation would be requested to complete.

Was this page helpful?
0 / 5 - 0 ratings