cordova-plugin-background-mode: SetInterval/SetTimeout stops working after 5 mins

I have a requirement of a timer, that should send out notification (I am used onesignal API notification) after a particular user set value is reached.

For example if a user sets a value of 7 mins, the timer should send notification after 7 mins. The time a user can set varies from 1-59 mins. But even with using the background mode plugin, I cannot get the setInterval/setTimeout functions to work after 5 mins.

Tried using recursive setTimeout method:


function startTimerCounter() {
this.timerCounter = 0;
const objThis  = this; //store this.

if(this.timerCounter >= this.setTime) {
this.backgroundMode.disable();
this.goalTimerReached = true;
}
else {
this.backgroundMode.enable();
this.timer= setTimeout(function request() {
       if (objThis.timerCounter=== objThis.setTime) {
         //onesignal notification
         clearTimeout(objThis.timer);
       } else {
         ++objThis.timerCounter;
         objThis.timer= setTimeout(request, 1000);
       }
     }, 1000);
}

Tried using the setInterval method:

function startTimerCounter() {
this.timerCounter = 0;
const objThis  = this; //store this.

if(this.timerCounter >= this.setTime) {
this.backgroundMode.disable();
this.goalTimerReached = true;
}
else {
this.backgroundMode.enable();
this.timerCounter = 0;
const objThis  = this; //store this.
this.timer= setInterval(() => {
        if (objThis.timerCounter=== objThis.setTime) {
          //onesignal notification
          clearInterval(objThis.timer);
        } else {
          ++objThis.timerCounter;
        }
      }, 1000);
}

The background activation notification at the top and I can see that the background mode is active. But the timer doesn’t seem to be running after 5 mins.

any idea how this could be solved?

About this issue

Most upvoted comments

I needed to call disableWebViewOptimizations() in Cordova’s pause event. The 5min limit there is from the WebView itself throttling things down after a certain amount of time not being visible.