title: Get working Ionic + ngCordova + background geolocation
body: <p><strong>Goal of the app:</strong> get geolocation on each move and log location either when app is in foreground and background.</p>
  
  <p>I've tried so many code and combination but I can't manage to have it working (2 days from now...). </p>
  
  <p>The classic geolocation (getCurrentPosition) is working fine but when we close the app the background geolocation is launched but nothing happen... Function "callbackFn" is never fired.</p>
  
  <p>I'm testing on IOS with xcode > Capabilities Audio &amp; location activated for background activity. I also made working the jQuery sample example given in plugin so I saw it working but never with ionic/angularjs.</p>
  
  <p>Here is the current controller handling the background:</p>
  
  <pre><code>.controller('TestCtrl', function($scope, $timeout, $cordovaBackgroundGeolocation, $ionicPlatform, $window)
  {
  $scope.lat_geo = "loading lat...";
  $scope.long_geo = "loading long...";
  
  
  //-- Geolocal launch
  var options = {
      enableHighAccuracy : false,
      desiredAccuracy: 0,
      stationaryRadius: 1,
      distanceFilter: 5,
      notificationTitle: 'Background tracking', // &lt;-- android only, customize the title of the notification
      notificationText: 'ENABLED', // &lt;-- android only, customize the text of the notification
      activityType: 'AutomotiveNavigation',
      debug: true, // &lt;-- enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false // &lt;-- enable this to clear background location settings when the app terminates
  };
  
  $ionicPlatform.ready(function()
  {
      console.log("[IONIC PLATFORM IS NOW READY]");
  
      //-- First launch a basic geolocalisation to get user acceptance of geosharing ;)
      navigator.geolocation.getCurrentPosition(function(location) {
              console.log('[GEOLOCAL JS1] Location from Phonegap');
      },
      function (error){
              console.log('[GEOLOCAL JS1] error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
      },options);
  
      //-- test adaptation depuis l'app jquery
      var callbackFn = function(location) {
              console.log('[BackgroundGeoLocation] Update callback:  ' + location.latitude + ',' + location.longitude);
      };
  
      var failureFn = function(error) {
              console.log('[BackgroundGeoLocation] Error: '+error);
      };
  
      $cordovaBackgroundGeolocation.configure(callbackFn, failureFn, options);
  
      // Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
      $cordovaBackgroundGeolocation.start();
  
      //-- Just a timeout to retreive long / lat
      $timeout(function()
      {
          navigator.geolocation.getCurrentPosition(function(location)
          {
              console.log('[GEOLOCAL JS3] Location from Phonegap');
              startPos = location;
              $scope.$apply(function () {
                  $scope.lat_geo = startPos.coords.latitude;
                  $scope.long_geo = startPos.coords.longitude;
              });
              console.log("[GEOLOCAL BASIC] OK this time :)");
          },
          function (error){
              console.log('[GEOLOCAL JS3] error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
          },options);
      }, 3000);
  
  });
  //-- End Geolocal
  })
  </code></pre>
  
  <p>I've put all my code (a complete ionic app starter) on github: <a href="https://github.com/Jeff86/ionic_ngcordova_backgroundgeo_test/tree/master" rel="nofollow">https://github.com/Jeff86/ionic_ngcordova_backgroundgeo_test/tree/master</a></p>