I need to fetch data from server & put into local DB in background

I need to fetch data from server & put into local DB in background. I mean this syncing should happen automatically in night. I am using SQLite as local app DB.

You can use $interval angular service for that.

  • register getDataFromBackend function on app.js (onReady) or app’s main controller with $interval service

  • getDataFromBackend should getting data from backend and return promise, then resolving promise with format recived data and save data to the sqlite database.

      function updatePrograms() {
              return Database.getCategoryLastUpdateDate().then(function (date) {
                  Backend.getProgramList(date).then(function (res) {
                      return res == null ? $q.when(null) :
                      Database.savePrograms(res.data).then(function () {
                          $log.debug('Updater: update Programs done');
                          return res;
                      });
                  });
              });
          };
    

    $interval(function() {
    updatePrograms();
    }, 5601000);

But i am guessing your app should be running for this, if not you can update data on app start.

If this is not enough and you want to update data when app is closed, you can write your own plugin with broadcastReciever (for android) implementation like this.

Hey thanx for the insights. But with background I meant when I am not on my App.

The code sent by you is running fine when I am accessing my app’s other screens but that is not my requirement.

Try to use cordova-plugin-background-mode or cordova-plugin-background-download. Also you can write custom plugin.