Cordova background mode plugin not getting activated?


#1

Hey guys,

I am facing a problem while using the background mode plugin as in it doesn’t get activated when the app is minimized. I am trying to use it for the first so i may have been making some stupid error.

Here is the code

document.addEventListener('deviceready', function() {
 
  
    $scope.position = {
        Lat : 0,
        Lon : 0
    };

    var id= $SessionService.get('id');


  
    cordova.plugins.backgroundMode.setDefaults({ text: 'Doing heavy tasks.' });
    cordova.plugins.backgroundMode.enable();
    cordova.plugins.backgroundMode.onactivate = function () {
        console.log("sdasdasdasdasdasd");
    };

    cordova.plugins.backgroundMode.onfailure = function (errorCode) {
        console.log(errorCode);};
    //Ping the status every 10 seconds
    var pingCheck = function () {


        var OnLocationSuccess = function (resp) {
            console.log("Location retrieved successfully");
            console.log("The current accuracy is " + resp.coords.accuracy);
            console.log("Current location is", resp);
            $scope.position.Lat = resp.coords.latitude;
            $scope.position.Lon = resp.coords.longitude;
            var OnPingSuccess = function (resp) {
                console.log("Current status is ", resp.data.status);
                if (resp.data.status != 'Online') {
                    $SessionService.store('status', resp.data.status);
                    $InterfaceService.Set(resp.data.status);
                }
            };

            var OnPingFailure = function (reason) {
                console.log("Couldn't ping because ", reason);
                if (reason.data.message = "Offline") {
                    $InterfaceService.Set("Offline");
                    $interval.cancel(pingInterval)
                }
            };

            $ApiService.PingStatus(driverId, $scope.position.Lat, $scope.position.Lon).then(
                OnPingSuccess, OnPingFailure);
        };

        var OnLocationFailure = function (reason) {
            console.log("Cannot get the location because of ", reason);
        };
        console.log("Background service status running : "+
            cordova.plugins.backgroundMode.isEnabled()+"\n Active : "+
            cordova.plugins.backgroundMode.isActive());
        $LocationService.getLocation().then(OnLocationSuccess, OnLocationFailure);


    };
    pingCheck();
    var pingInterval = $interval(pingCheck, 5000);
});
});

As you can see that i am running an interval to call a service that does a http request and sends the location to the server.
Now what i want to achieve is when the app is minimized the interval will keep sending the pings to the server. Currently it tries to send the location but cannot get the geolocation when the app is minimized. Also when i check the isActive() properly of background service, It’s status is still false when the app is minimized. Can you tell me what i could be doing wrong?

ANY HELP IS APPRECIATED, THANK YOU!