Options to work with Background Tasks in 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:

  1. Insomnia
  2. Background Task
  3. $Interval
  4. Cordova Background Mode Plugin
  5. 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.

Add comment