In need of a timer that can send notifications via the OneSignal API after a user-defined time period is reached. Users can set the timer for any value between 1-59 minutes. Despite attempts to use the background mode plugin, specifically setInterval and setTimeout functions beyond 5 minutes seem to be problematic.
One approach involved 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);
}
}
An alternative attempt utilized 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 displays at the top indicating active background mode. However, the timer does not appear to function correctly past the 5-minute mark.
Any suggestions on how to resolve this issue?
*** Update ****
Trying to trigger the function every 4 minutes in the background to ensure continuous operation but facing challenges:
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);
this.backgroundMode.on('activate').subscribe(() => {
setInterval(function () {
this.startTimerCounter();
}, 240000);
})
}
}