Ohai.
I wrote the original article for @jscrambler .
I can think of two ways.
You can achieve the functionality on Android using setInterval .
You will need unique id for LocalNotifications so we need to set a variable beforehand. Here is the code snippet for the functionality.
id:number=0;
lettaskId=BackgroundTask.beforeExit(async()=>{letnotificationInterval=setInterval(async()=>{letlocation=awaitthis.getCurrentPosition();LocalNotifications.schedule({notifications:[{title:"Last Known Location",body:"Latitude: "+location.coords.latitude+"Longitude: "+location.coords.longitude,id:this.id++,schedule:{at:newDate(Date.now()+1000*10)},sound:null,attachments:null,actionTypeId:"",extra:null}]});if(this.id>10){console.log("task Finished",this.id)clearInterval(notificationInterval);BackgroundTask.finish({taskId});}else{console.log("task in progress",this.id)}},2000)});
Apparently setInterval and setTimeout don't work in background on iOS.
So the other way is more hacky as shown in Ionic Docs. This should work on both Android and iOS.
lettaskId=BackgroundTask.beforeExit(async()=>{letstart=newDate().getTime();for(vari=0;i<1e18;i++){if(this.id>10){break;}if((newDate().getTime()-start)%2000===0){letlocation=awaitthis.getCurrentPosition();LocalNotifications.schedule({notifications:[{title:"Last Known Location",body:this.id+" Latitude: "+location.coords.latitude+"Longitude: "+location.coords.longitude,id:this.id++,schedule:{at:newDate(Date.now()+1000*10)},sound:null,attachments:null,actionTypeId:"",extra:null}]});}}BackgroundTask.finish({taskId});});
I think that should suffice your usecase.
Cheers !
You can't. The time limit for iOS is 180 seconds and android implementations vary a lot. Doze mode, battery optimizations, and things like that. You can check this website to check the different vendor implementations.
Depending on the use case you can design custom solutions.
One of the ways to actually force an app in foreground via silent push notifications and native background tasks. Or a combination of silent and full push.
However, this is not possible in Ionic/Cordova/Capacitor. As such you will have to dabble into Native Programming.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Jscrambler, this really a nice tutorial, I just want to know, how to run a timer/ timer in angular.
Actually i wanna run this snippet periodically in the background:
Thanks.
Ohai.
I wrote the original article for @jscrambler .
I can think of two ways.
You can achieve the functionality on Android using
setInterval
.You will need unique id for LocalNotifications so we need to set a variable beforehand. Here is the code snippet for the functionality.
Apparently
setInterval
andsetTimeout
don't work in background on iOS.So the other way is more hacky as shown in Ionic Docs. This should work on both Android and iOS.
I think that should suffice your usecase.
Cheers !
You just saved me from a bunch of googling, very nice examples! Thank you very much!
how about to call the function every minute, for example, every 5 minutes?
You can't. The time limit for iOS is 180 seconds and android implementations vary a lot. Doze mode, battery optimizations, and things like that. You can check this website to check the different vendor implementations.
Depending on the use case you can design custom solutions.
One of the ways to actually force an app in foreground via silent push notifications and native background tasks. Or a combination of silent and full push.
However, this is not possible in Ionic/Cordova/Capacitor. As such you will have to dabble into Native Programming.