Hello,
I’m using these two plugins to update the location of my users in background
https://github.com/apache/cordova-plugin-geolocation
https://github.com/christocracy/cordova-plugin-background-fetch
The Geolocation plugin works well when the application is executed in foreground, but when i simulate a background fetch from Xcode (debug>Simulate Background Fetch), the plugin return the error code 2 corresponding to PositionError.POSITION_UNAVAILABLE.
Here is my Background task:
// This is in $ionicPlatform.ready callback
var Fetcher = window.BackgroundFetch;
Fetcher.configure(function() {
console.log('BackgroundFetch initiated');
GeolocationService.updateLocation().then(function(coords){
console.log("BackgroundFetch: Location OK");
API.heartBeat().then(function(){
console.log("Heartbeat sent");
Fetcher.finish();
},function(){
console.log("Heartbeat failed");
Fetcher.finish();
});
},function(){
console.log("BackgroundFetch: Location failed");
Fetcher.finish();
})
},function() {
console.log('- BackgroundFetch failed');
},{
stopOnTerminate: false
});
“API.heartBeat” is just a promise that send the location of the user to my remote API
And the method updateLocation of my GeolocationService :
updateLocation: function(){
var def = $q.defer();
$ionicPlatform.ready(function() {
$cordovaGeolocation.getCurrentPosition({
timeout : 10000,
enableHighAccuracy: false,
maximumAge: 5 * 60 * 1000
}).then(function (position) {
console.log("Position retreived: lat:" + position.coords.latitude +", lng:" + position.coords.longitude);
$rootScope.lat = position.coords.latitude
$rootScope.lng = position.coords.longitude
$rootScope.geolocationDeny = false;
def.resolve({lat: $rootScope.lat, lng: $rootScope.lng})
}, function(error) {
console.error("Geolocation Service Error: " + error.code);
if(error.code == 1){
$rootScope.geolocationDeny = true;
}
def.reject();
});
});
return def.promise;
}
I hope someone has already faced this issue…
Thanks