19. March 2021
Namrata
Angular , Ionic
For the last 2 weeks, I am trying to find ways to deal with background tasks in ionic. I have found 5 different options to work with background tasks mentioned below:
- Insomnia
- Background Task
- $Interval
- Cordova Background Mode Plugin
- ngx-cron-jobs
Below is some of the information you will need to use:
Note: I have given the Installation commands for the ionic capacitor project only.
Name |
Plugin/Method |
Installation Commands |
Provider/ Module |
Comments |
Insomnia |
Cordova plugin |
$npm install cordova-plugin-insomnia $npm install @ionic-native/insomnia $ionic cap sync |
Provider- Insomnia, (app.module.ts) import {Insomnia} from '@ionic-native/insomnia/ngx' provider:[.., Insomnia,..] |
Insomnia Offers these two methods: keepAwake()- It prevents the app to go inactive, allowSleepAgain()- It makes the app go on an inactive state |
Background Task |
Capacitor Plugin |
No need to install |
|
Background Task offers below two methods: beforeExit()- background task execution starts and executes it till finish()- Call to end the task |
Interval |
Method |
No need to install |
|
|
Cordova Plugin Background Mode |
Cordova plugin |
$npm install cordova-plugin-background-mode $npm install @ionic-native/background-mode $ionic cap sync |
Provider- BackgroundMode, (app.module.ts) import {BackgroundMode} from '@ionic-native/background-mode/ngx' provider:[.., Insomnia,..] |
Cordova Plugin Background Mode offers a set of methods to deal with background tasks. Some of them are: enable()- to enable background mode on('EVENT', function), isActive()- To check the app state(foreground/ background) |
ngx-cron-jobs |
Method |
$ npm install ngx-cron-jobs@0.x.x --save |
import { CronJobsModule } from 'ngx-cron-jobs'; @NgModule({ imports: [...,CronJobsModule,...] }) export class AppModule { } |
|
I have summarized my findings here. Shortly I will share some sample projects on some of the above plugins/methods in my upcoming blogs.
I hope the above information is helpful to you, Thank you.