简体   繁体   中英

Why doesn't the cordova-plugin-background-mode plugin work on android?

I'm trying to send the geolocation by ajax in intervals of 30 seconds in the background, since this application is being used to know the "real time" location of the deliverers at all times. The problem is that when you enable the plugin in the android version when generating the apk and installing it on the cell phone it doesn't work, even when you put the application in the background and put it back in the foreground it restarts completely. These are the versions that I am using for the development of the application:

  • Cordova 10
  • Nodejs 14.16
  • JQuery 3.5

Structure:

  • js
    • login.js
    • home.js
  • index.html -- this is the login page where the login.js file is located
  • home.html -- this is the home page where the home.js file is located

login.js

document.addEventListener('deviceready', function () {
  cordova.plugins.backgroundMode.enable();
});

home.js

let isSending = false;
let intervalId = null;
let email = window.localStorage.getItem("user_email");
let token = window.localStorage.getItem("token");
let path = window.localStorage.getItem("api_url");

let onMapSuccess = function (position) {
    let latitude = position.coords.latitude;
    let longitude = position.coords.longitude;
    if (!isSending) {
        $.ajax({
            type: "GET",
            url: path + "/geoTransportista/" + email + "/" + latitude + "/" + longitude,
            headers: {
                Authorization: "Bearer " + token,
                "Content-type": "application/json",
            },
            beforeSend: function() {
                isSending = true;
            }
        }).done((res) => {
            if (res.state == "successful") console.log("ENVIO EXITOSO");
        }).fail((err) => {
            console.log(err);
        }).always(() => {
            isSending = false;
        });
    }
};

let onMapError = function (error) {
    isSending = false;
}

let getLocation = () => {
    navigator.geolocation.getCurrentPosition(onMapSuccess, onMapError, {
        enableHighAccuracy: true,
    });
    return getLocation;
}
document.addEventListener('deviceready', function (e) {
  intervalId = setInterval(getLocation(), 30000);
  cordova.plugins.backgroundMode.on('activate', function (e) {
        cordova.plugins.backgroundMode.disableWebViewOptimizations();
        if (intervalId) clearInterval(intervalId);
        intervalId = setInterval(getLocation(), 30000); 
  });
  cordova.plugins.backgroundMode.on('deactivate', function (e) {
        if (intervalId) clearInterval(intervalId);
        intervalId = setInterval(getLocation(), 30000); 
  });
});

cordova-plugin-background-mode 在 Cordova 10 上不起作用。您可以尝试降级到 Cordova 9。回购似乎有一段时间不活动,不确定作者是否会修复它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM