cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

Set interval issue with IOS 11

Open desmeit opened this issue 8 years ago • 12 comments

I try to run a setinterval while the app is in background under IOS 11.

document.addEventListener('deviceready', function () {
	
	cordova.plugins.backgroundMode.enable();
	cordova.plugins.backgroundMode.onactivate = function () {
	console.log('Background Mode');
	};
}, false);

var interval = setInterval(function() {
// do some stuff
}, 1000);

But the interval is stopping if I go into background mode. Its not working. What Im doing wrong?

desmeit avatar Oct 16 '17 12:10 desmeit

I believe you need to have cordova-custom-config to be able to have the javascript code continue running in the background.

Have you tried that?

dimitriscsd avatar Oct 16 '17 13:10 dimitriscsd

Why do you think it is necessary? Which settings are necessary?

desmeit avatar Oct 16 '17 14:10 desmeit

In my case I needed it to be able to use the bluetooth-central plugin which I wanted to use for bluetooth communication in the background. Your case might be different but it might be worth a shot.

I've used the UIBackgroundModes setting for bluetooth-central. Here is a link to the plugin. You can see the reference to custom config in the section labeled Background Scanning and Notifications on iOS:

https://github.com/don/cordova-plugin-ble-central

I hope some of this info proves to be useful for you. Good luck.

dimitriscsd avatar Oct 16 '17 14:10 dimitriscsd

Thanks a lot.

desmeit avatar Oct 16 '17 14:10 desmeit

Unfortunately I have no luck. These options are for "audio", "location" etc. only, not for javascript functions in the background like setinterval.

It was working now if I enabled the bg-mode within my timer-function (not within deviceready). But this worked only once. If I clear this interval and start it again it is not working anymore.

desmeit avatar Oct 16 '17 15:10 desmeit

So if you put a device ready listener in the .run() of the module, and you enable the background mode there, as well as start the interval, does it not work when you go in the background?

The initialization of the interval has to be in an app wide scope so that it doesnt dissapear when you go somewhere else.

I have a timer in my app that starts in the .run of the module and it works fine. However I never stop/start it again while the app is running.

dimitriscsd avatar Oct 16 '17 15:10 dimitriscsd

Yes, if I enabled it in deviceready it is not working, but if I enable it in another function and start it, it is working for the first time. The interval is running for example 10 sec, after that it is stopping and waits for the next click. Then it starts again and is running 11 sec for example:

function timer( next ){
    cordova.plugins.backgroundMode.enable();
    var interval = setInterval(function() {
        var minutes = parseInt(timer[0], 10);
	  var seconds = parseInt(timer[1], 10);
	  --seconds;
        //...some functions...
       timer2 = minutes + ':' + seconds;	

     if ( timer2 == "00:00" ){
          clearInterval(interval);
     }

     }  , 1000 );

} // function timer end

$(document).on('click', '.next', function() {
         timer(next);
})

this is running only the first time. if I click again, the background mode is not running anymore.

desmeit avatar Oct 16 '17 15:10 desmeit

I found the same question on stackoverflow:

https://stackoverflow.com/questions/46179744/setinterval-functions-not-running-when-app-in-background

It would be nice if the author of the plugin could give an idea how it could work.

desmeit avatar Oct 17 '17 08:10 desmeit

@katzer Could you help us?

desmeit avatar Oct 23 '17 08:10 desmeit

One way around it would be to use a Cordova plugin to call native code for the timer. I posted an answer in the StackOverflow question with some details of this: https://stackoverflow.com/questions/46179744/setinterval-functions-not-running-when-app-in-background

uniphonic avatar Oct 24 '17 18:10 uniphonic

@desmeit do you find a solution? I'm in the same boat :(

tizianozonta avatar Nov 10 '17 14:11 tizianozonta

Hi all, since you are experts about BLE, ionic and Background, I have the following issue:

https://github.com/don/cordova-plugin-ble-central/issues/850

Any advise or pointer would be greatly appreciated, thanks a lot!

giowild avatar May 03 '21 07:05 giowild