title: Cordova Background GeoLocation Plugin
body: <p>I am using this cordova plugin <a href="https://github.com/christocracy/cordova-plugin-background-geolocation/" rel="nofollow">https://github.com/christocracy/cordova-plugin-background-geolocation/</a> for an ios app, but running into some issues. Would appreciate if someone can help.</p>
  
  <p>Here is the sample code I am using from the plugin example itself. </p>
  
  <p>Issue is the callback function (callbackFn) doesn't seems to get called when a location change is detected, but I do get notifications on Iphone and I see one of the notification as Outstanding bg-task was force killed. Not sure what exactly this mean...</p>
  
  <pre><code>var app = {
      // Application Constructor
      initialize: function() {
          this.bindEvents();
      },
       bindEvents: function() {
          document.addEventListener('deviceready', this.onDeviceReady, false);
      },
  
  onDeviceReady: function() {
      app.receivedEvent('deviceready');
       if (window.plugins.backgroundGeoLocation) {
          app.configureBackgroundGeoLocation();
        }
  },
  // Update DOM on a Received Event
  receivedEvent: function(id) {
      window.cache.clear( function(){console.log("cache cleared");}, function(){console.log("cache could not be cleared");} );
      require(['http://localhost:8080/sdm/app_resources/app_product_HY6I1FJ5KGRD/resources/js/main.js']);
  
  },
  configureBackgroundGeoLocation: function() {
          // Your app must execute AT LEAST ONE call for the current position via standard Cordova geolocation,
              //  in order to prompt the user for Location permission.
              window.navigator.geolocation.getCurrentPosition(function(location) {
                   console.log('Location from Phonegap');
               });
  
              var bgGeo = window.plugins.backgroundGeoLocation;
  
        /** This would be your own callback for Ajax-requests after POSTing background geolocation to your server.*/
  
              var yourAjaxCallback = function(response) {
                      bgGeo.finish();
                  };
  
   /**This callback will be executed every time a geolocation is recorded in the background.*/
              var callbackFn = function(location) {
                  console.log('[js] BackgroundGeoLocation callback:  ' + location.latitudue + ',' + location.longitude);
                  // Do your HTTP request here to POST location to your server.
                   deviceId = 'I2FKRKXX8SQ0';
                   trackingToken = 'SomeSecurityToken';
                   jsonData={location:{longitude:location.longitude,latitude:location.latitude}};
                   var encodedToken = Base64.encode('tracker' + ':' + trackingToken);
                   var http = new XMLHttpRequest();
                   var url = "http://localhost:8080/sdm/v1/KC/host/device/"+deviceId;
  
                   var params = JSON.stringify(jsonData);
                   http.open("PUT", url, true);
  
                   //Send the proper header information along with the request
                   http.setRequestHeader("Content-type", "application/json");
                   http.setRequestHeader("accept", "json");
                   http.setRequestHeader("Authorization", "Basic "+encodedToken);
                   http.setRequestHeader("Content-length", params.length);
                   http.setRequestHeader("Connection", "close");
  
                  /* http.onreadystatechange = function() {//Call a function when the state changes.
                   if(http.readyState == 4 &amp;&amp; http.status == 200) {
                   alert(http.responseText);
                   }
                   }*/
                   http.send(params);
  
                  yourAjaxCallback.call(this);
              };
  
              var failureFn = function(error) {
                      console.log('BackgroundGeoLocation error');
                  }
  
              // BackgroundGeoLocation is highly configurable.
              bgGeo.configure(callbackFn, failureFn, {
                   desiredAccuracy: 10,
                   stationaryRadius: 20,
                   distanceFilter: 30,
                   activityType: 'AutomotiveNavigation',
                   debug: true, // &lt;-- enable this hear sounds for background-geolocation life-cycle.
                   stopOnTerminate: false
                 });
  
              // Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
              bgGeo.start();
  
              // If you wish to turn OFF background-tracking, call the #stop method.
              // bgGeo.stop()
          }
  </code></pre>
  
  <p>};</p>