Reputation: 1
I create an App on cordova, a social network to find friend on a map. The geolocation works fine when the app is on the foreground (using the cordova-plugin-geolocation plugin). But I want also it to work when the app is on the background. To do it, I'm using cordova-plugin-background-mode, working in this way:
var geolocID;
window.onload = function(){
document.addEventListener("deviceready", init, false);
document.addEventListener("resume", function() { console.log("resume with id :" + geolocID); navigator.geolocation.clearWatch(geolocID); init();},false);
document.addEventListener("pause", geolocBackground,false);//function() {setTimeout(function(){console.log("TEST");},5000)},false);
}
function geolocBackground()
{
console.log("In geolocaBackground " + ", ID = " + geolocID);
navigator.geolocation.clearWatch(geolocID);
console.log("ID = " + geolocID);
geolocID = navigator.geolocation.watchPosition(positionSuccessBackground,positionError,{timeout: 5000});
console.log("Geolocation end");
}
function init()
{
geolocID = navigator.geolocation.watchPosition(positionSuccess, positionError, {enableHighAccuracy: true, timeout: 60000, frequency: 10000});
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.onactivate = function(){console.log("Backgound mode activated");
cordova.plugins.backgroundMode.configure({silent: false});
};
cordova.plugins.backgroundMode.ondeactivate = function() {console.log("Backgound mode disabled");};
}
It seems that the bacgkound mode works, because when I put the app in pause, I have this in my console :
In geolocaBackground , ID = 707e54f8-fb99-fb36-2855-341b45f3d774
ID = 707e54f8-fb99-fb36-2855-341b45f3d774
Geolocation end
resume with id :707e54f8-fb99-fb36-2855-341b45f3d774
But the geolocation didn't work, returning a Timeout error, code 3.. I don't understand what is wrong with my code, I didn't find anything on the web. It seems that the app can't access to the location information of the phone in the background. The code is executed, but it's only the location function that doesn't work.. If somebody has some knowledge with this issue, it will be greatly appreciated :) Thanks for your help, regards
Upvotes: 0
Views: 1043
Reputation: 1
Thanks for your answers. I didn't succeed to make the geolocation work in background, with or without chrome, or in walking a few meters. I solve it myself, by coding an Android service and a gps tracker in Java, so my App still tracks my users when the app is closed.
Upvotes: 0
Reputation: 11
Its a chromium bug, please look at:
https://bugs.chromium.org/p/chromium/issues/detail?id=585055
Upvotes: 1
Reputation: 31
Hopefully this is the same issue I was having. My code is as follows:
start() {
cordova.plugins.backgroundMode.enable();
var gpsOptions = { maximumAge: 2000, timeout: 5000, enableHighAccuracy: true };
document.addEventListener('deviceready', function () {
var GpsLog = [];
this.gpsChecker = Observable.timer(0, 4000);
this.gpsChecker.subscribe(() => {
navigator.geolocation.getCurrentPosition(position => {
var deviceLog = {
type: 'Device Location',
value: position.coords.longitude + "," + position.coords.latitude
}
this._api.uploadLog(deviceLog);
}, null, gpsOptions);
});
});
}
(I'm using ionic 2 and typescript but the principal is the same). The issue seems to be (well mine anyway) was that when you app is in the background it doesn't have the 'priority' access to the devices GPS location. I.e when my app goes to background I have to walk around outside (about 15 meters) before I get a geolocation, where if the app is running in the foreground I get a geolocation every few seconds. Try lowering your timeout and your max age parameters and taking the device for a walk in clear skies.
Upvotes: 0