pierrot10
pierrot10

Reputation: 139

How can I use cordova-plugin-background mode

I need some assistance to make my App working in background. I created my first App with Cordova 10, jquerymobile and it works fine excepted in backgound.

The idea is to installed the following plugin cordova-plugin-background-mode, but I get some difficulties to understand how.

I installed the plugin and then I added the following

document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
    document.getElementById('deviceready').classList.add('deviseIsReady');
    cordova.plugins.backgroundMode.enable();

    if(Pages.checkConnection() == true)
        {
            Maps.load(); 
            setInterval(Maps.load, 300000);   
        }
}

In that way, the mobile is ready, the background mode is actived. I upload my App to my Android but my App stop working.

I wonder, if this is would be a solution

$(window).load(function(){

    document.addEventListener("offline", Pages.offLine, false);
    document.getElementById("refresh").addEventListener("click", Maps.refresh);
    document.getElementById("ffield").addEventListener("change", Maps.field);
    document.getElementById("fstations").addEventListener("change", Charts.changeStation);
    document.getElementById("threshold").addEventListener("change", Pref.threshold);
    document.getElementById("ffieldpref").addEventListener("change", Pref.field);
    cordova.plugins.backgroundMode.on('EVENT', backgroundEvent);
    
});


function backgroundEvent(){
    if(cordova.plugins.backgroundMode.isActive()){
        cordova.plugins.backgroundMode.moveToBackground();
    }
    else
    {
        cordova.plugins.backgroundMode.moveToForeground();
    }
}

But I do not know when, it will be actived and if it will work with IOS devise.

Some of you have an experience with Cordova and how to setup the background mode with jQuery Mobile? Any examples?

Many thank for your help and suggestion.

Cheers

Upvotes: 2

Views: 5975

Answers (2)

Roy Fagon
Roy Fagon

Reputation: 1

SOLVED: I had crash issues in my REACTJS->Cordova when it was enabled and I put the app in background mode. I corrected it by including this in my config.xml Config.xml

Upvotes: 0

Uncle Mike
Uncle Mike

Reputation: 21

You cannot use katzer's cordova-plugin-background-mode with Cordova 10. Works fine on 9.0, but that's as high as it goes. If your Builder program allows you a choice of Cordova versions, pick 9. I have been unable to find an adequate substitute for the plugin that works in 10.

Also in Android, check your config.xml or AndroidManifest.xml and make sure there is FOREGROUND_SERVICE permission. This is absolutely required for background operation.

Upvotes: 2

Related Questions