Creating background services in Ionic with Cordova or java

Asked

Viewed 2,186 times

3

I have an application that I started to do with Ionic. A priori will be compiled only for android. In this application the user will perform some registrations, which will be saved locally and every hour a service in the background should try to synchronize the database with a server (a post). I successfully completed all the steps and left that part of the background behind.

I’m using the plugin Cordova-plugin-background-mode to accomplish this task. However, even testing repeatedly all possible combinations, I realized that although the application is in the background and run what I need, at some point the android finishes the application (free memory for example). And this causes all services in the background to stop running until the application is opened and put in the background again.

I have already created services that run in background (services and Broadcasts receivers) with java for native android, but the current application is relatively large and a change now would be unviable.

My code, in the app.js file is like this:

angular.module('starter', ['ionic', 'ngCordova', 'ngStorage', 'ionic-material'])

  .run(function ($ionicPlatform, LocalizacaoService, $http) {

    $ionicPlatform.registerBackButtonAction(function (e) {
      e.preventDefault();
    }, 1000);

    $ionicPlatform.ready(function () {
      if (window.cordova && window.cordova.plugins.Keyboard) {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);

        // Don't remove this line unless you know what you are doing. It stops the viewport
        // from snapping when text inputs are focused. Ionic handles this internally for
        // a much nicer keyboard experience.
        cordova.plugins.Keyboard.disableScroll(false);
      }
      if (window.StatusBar) {
        //StatusBar.styleDefault();
        StatusBar.styleLightContent();
      }

    })

    document.addEventListener('deviceready', function () {

      cordova.plugins.backgroundMode.overrideBackButton();
      cordova.plugins.backgroundMode.excludeFromTaskList();

      //cordova.plugins.backgroundMode.configure({silent: true});
      cordova.plugins.backgroundMode.setDefaults({
        title: "My App",
        text: "Something here",
      });
      // Enable background mode
      cordova.plugins.backgroundMode.enable();
      cordova.plugins.backgroundMode.isActive();

      cordova.plugins.backgroundMode.on('activate', function () {
        cordova.plugins.backgroundMode.disableWebViewOptimizations();
      });

      // Run when the device is ready
      // Called when background mode has been activated
      cordova.plugins.backgroundMode.onactivate = function () {

        //localizacao a cada  5 min
        //localiza, salva e envia tudo
        setInterval(function () {
          //executa uma tarefa   
          console.log("executou")

        }, 10000);
        //sinc de visitas a cada 30 min
        setInterval(function () {
          //executa outra
          console.log('executou');

        }, 18000);
      }

    }, false);

  });

My questions are: is my plugin implementation wrong? or, there is the possibility that after the android code is created, a service with native android be implemented in the code?

  • 1

    I’m in Ionic 2 that has nothing to do with Ionic 1, but I believe, as in the native Android, you have wake up your service once in a while, because it ends, unbelievably sleeping. In this plugin there that you are using has the cordova.plugins.backgroundMode.wakeUp(); ... Take a look at how to do this there at https://github.com/katzer/cordova-plugin-background-mode

  • I came up with the idea of using wakeup(), but at what point do you think it would be feasible to perform this? Because if the application is closed/dead it will not be executed. right?

  • Can you find out when it closes? If not, run from minute to minute and see if it weighs too much.

  • Maybe you should think about syncing your app to your bank only when it’s in use, leaving it as a background service making request is not something advisable and much less cool since you will use your user’s band consistently without him knowing/wanted and not counting on the more energy consumption that your application will generate, these two factors can generate a very large user leak since it might not be hard to realize that it was after the installation of your app or by the usage graphics of android battery and band.

  • I recommend you take a look at this example: https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample I believe this is exactly what you want to do.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.