Search code examples
javascriptcordovacordova-plugins

Cordova Android- Keep sending AJAX request to the server while the device is in background mode


I need to keep performing Ajax request to the server even if the device is in background, I’ve tried the “cordova-plugin-background-mode “ https://github.com/katzer/cordova-plugin-background-mode , Please see the code below ,but for some reason it doesn’t work

Any help would be appreciated to solve this problem

cordova.plugins.backgroundMode.on('activate', function (){
  window.setInterval(function () {
    //Ajax request every 6 S
    }, 6000)
});

Solution

  • It stops sending the Ajax request when the device is in background mode; I need this to check if the user still logged in his/here account

    Basically you want your app to consider your users as online as long as they have started the app and keep their phones on? If this is true, then you can simply assume they will stay online forever.

    When your app is sent to the background, you must do what any other app does: Consider the user is no longer online. Do not waste user resources and network data from their carrier just to see if the user's phone is still on, this makes no sense at all.

    Whenever the app is active and running, you update your database on every request, or every few minutes with a timer if there's no user-initiated network activity.