Ionic Background Fetch


Ionic Background Fetch Native Ios – Background fetch is basically IOS api which wakes up your app about every 15 minutes and provides your app exactly 30s of background running time. You can define the callback function which will be called when the background-fetch event will occur. Here in this tutorial we are going to explain how you can install this plugin to use the IOS native background-fetch feature.


Ionic Background Fetch Native Plugin | Install | Example

You can install the background fetch plugin simply as below-

Installation

To install the cordova-plugin-background-fetch plugin run the below command-

Ionic Background Fetch Native Plugin Installation:

$ ionic cordova plugin add cordova-plugin-background-fetch
$ npm install --save @ionic-native/background-fetch

Supported Platforms

  • IOS – This is only supported on Ios.

Example

Here is an example of background fetch –

Ionic Background Fetch Example:

import { BackgroundFetch, BackgroundFetchConfig } from '@ionic-native/background-fetch';


constructor(private backgroundFetch: BackgroundFetch) {

  const config: BackgroundFetchConfig = {
    stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true.
  };

  backgroundFetch.configure(config)
     .then(() => {
         console.log('Background Fetch initialized');

         this.backgroundFetch.finish();

     })
     .catch(e => console.log('Some Error during initializing background fetch', e));

  // Start the background-fetch API. Your callbackFn provided to #configure will be executed each time a background-fetch event occurs. NOTE the #configure method automatically calls #start. You do not have to call this method after you #configure the plugin
  backgroundFetch.start();

  // Stop the background-fetch API from firing fetch events. Your callbackFn provided
 backgroundFetch.stop();

}

Instance Members

  • configure(config) – Ccnfigure the plugin’s fetch Callback.
  • start()– Start the background fetch.
  • stop()– Stop the background fetch.
  • finish()– When fetch action is complete.
  • stop()– Returns the background fetch status.

Background Fetch Configuration

BackgroundFetchConfig

  • BackgroundFetchConfig(stopOnTerminate) stopOnTerminate param is boolean, Set true to cease background-fetch.

Advertisements

Add Comment

📖 Read More