Ionic 4 | Keep It Running in Background using Cordova and Ionic Native plugins

In a native application like android, we have various approaches to keep a service or operation running while the application is minimized or not in the foreground. But in a hybrid application like built-in Ionic framework, we have some limitation to do that as the applications are not that much flexible to use background services or any broadcasts attached to some working threads.

In Ionic, luckily we have a Cordova Plugin, which helps in doing this to some extent. We can keep our application in Running state even if a user minimizes it or moves it in the background.

So here we will implement a plugin BackgroundMode in Ionic’s latest version 4. Ionic 4 is still in beta phase, but it will be released soon for development.

Let’s get started!

First, we will create a new Ionic 4 Application

Make sure you have NodeJS and latest version of Ionic CLI installed

Run the following command to create a new Ionic 4 Application

Install the Cordova and Ionic Native plugins

 

Import native plugins in app.module.ts

After imports and adding, providers for added plugins file will look like this

 

Now you need to call  backgroundMode.enable() method to enable background service in your application.

Here we will call this method in the Application Component to start it on application start.

Now you can run the application in a real device. Create a debug apk for Android by running

This plugin will keep the app running in the background.

Similarly, we can disable BackgroundMode by calling this method

To check if BackgroundMode is active we can run the following method

You can check more events available here .

 

Leave a Reply

avatar
  Subscribe  
Notify of