Location detection and HTTP call on background mode using cordova-plugin-background-mode

725 views
Skip to first unread message

Aayush Bhandari

unread,
Mar 20, 2016, 8:23:53 PM3/20/16
to phonegap
http://stackoverflow.com/questions/36108289/location-detection-and-http-call-on-background-mode-using-cordova-plugin-backgro

Any help is appreciated. Thank You


I have an ionic app in which the app behaves in the following way when it is in the foreground. A service in the app checks geolocation every x seconds using navigator.geolocation and based on the coordinates it calls another service to do some http calls. Now these things obviously don't work when the app is in the background, but it is very crucial that my app is able to do these detections and make these calls (Which includes changing some variables in the service.js)

I have the following code in my app.js so far.

document.addEventListener('deviceready', function () {
    // Android customization
    cordova.plugins.backgroundMode.setDefaults({     title:  "XYZ",
    ticker: "XYZ is running on background",
    text:   "XYZ is keeping track now on the background. This is a test message"});
    // Enable background mode
   cordova.plugins.backgroundMode.enable();

cordova.plugins.backgroundMode.onfailure = function(errorCode){
    console.log("failed")
};

// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {

      console.log("Hello from background!); // works fine


      ////////////////// This piece of code doesn't execute /////////////
      function success(position){
       console.log("In success function");
       MyService.doStuff(); // This will make some http calls
      }

       function success(position){
       console.log("In success function");
       MyService.doStuff(); // This will make some http calls
      }

       function error(error){
       console.log("In error function");
      }

       var options = {timeout: 60000, enableHighAccuracy: true}; // options for checking geolocation

       navigator.geolocation.getCurrentPosition(success, error, options); // eventually I'd like to add setInterval to this piece, so it executes every x seconds
      //////////////////////////////////////////////////////////////////

}

cordova.plugins.backgroundMode.ondeactivate = function() {
  console.log("Bye Bye from Background!"); // works fine
};


}, false);

If this works I should be able to wrap the piece in a function, and setInterval, so the location gets checked every x seconds. Can anyone suggest how I can make this work on the background. It works fine when I do this on the foreground.

Rob Willett

unread,
Mar 21, 2016, 3:13:15 AM3/21/16
to phonegap
You haven't told us if the App is Android or iOS. They have different methods of doing this sort of thing. Read the first page on this group and provide more information.  Having a comment that states Android configuration is not definitive. 

If you are targeting Apple you cannot do what you are wanting to do the way you want to do it. As it very clearly states on Katzers website, the use of backgroundMode plugin focusses on Enterprise wide distribution and Apple will not allow it. Read the issues and discussion on the backgroundMode Github board.

Android is a little more flexible, but the last time we looked at the iOS 'standard' geolocation plugin, it did not support working in the background properly. Things may have changed. We use a paid for plugin from transistorsoft (https://github.com/transistorsoft) which does proper background and foreground geolocation. This works very, very well and *might* be free for your use case. I would strongly advise you to look at that. We spent months last year messing around with the standard one and trying to get background mode working the way we wanted it and gave up. Its OK for foreground but doesn't work that well in background. Also on IOS it will stop working anyway after around 15 mins as your app gets suspended. Can't comment on Android as we never got that far.

Also your approach of a SetInterval and constantly polling for getCurrentPosition is not good practice unless the main use of your app is to drain your phone battery as much as possible as quickly as possible. The best way to do this is to wait for the GPS position to change and capture the change in position through a variable. You can still use a timer to check if the position variable has changed but don't keep polling the GPS.

Rob

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages