Specimen
Specimen

Reputation: 76

How to make a background process when app is not running in Cordova?

I am trying to do some functionality when app is suspended. I used -

https://github.com/katzer/cordova-plugin-background-mode

It works perfectly when app is running, when killed the app is stop working. Then, I used - https://github.com/katzer/cordova-plugin-local-notifications

It make a notification when app is killed, but I can't handle any of it's events unless app is running. Does anyone try to do like an ajax call when app is killed after period of time? Is there any other repos to make a background service while app isn't running?

(Push notifications are not a solution for me in this scenario)

Thanks.

Upvotes: 1

Views: 2229

Answers (1)

pherris
pherris

Reputation: 17723

You are looking for this plugin: https://ionicframework.com/docs/native/background-fetch/ which allows you to run 30s of code in the background during the user's "active" time of day (as defined by Apple).

  const config: BackgroundFetchConfig = {
    stopOnTerminate: true, // 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');
         //Your code to be repeatedly executed here

         this.backgroundFetch.finish();

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

To me the docs aren't clear but the method passed to the then is what is called at whatever interval the OS wakes up at.

Upvotes: 1

Related Questions