Cordova应用程序,每分钟将我的位置上传到后端服务器,即使屏幕关闭或应用程序在后台



我正在努力为Android编写一个Cordova应用程序,该应用程序将在屏幕关闭后继续运行。我正在尝试后台和前台服务(有时一起(来保持一个简单的循环运行:获取我的位置,并通过 TCP 将 JSON 发送到后端服务器。有人有解决方案吗?我正在运行 Android 8.1 Oreo,并且尝试在我的应用程序中同时放置前台、后台和后台运行的计时器 npm 包。无论我尝试什么,在关闭手机屏幕 ~5 分钟后,该应用程序都会停止与后端服务器通信。我已经禁用了省电措施,我真的被困在这里了。谢谢。

使用这些插件(使用 cordova 9.0.0(:

cordova-background-timer 0.0.4 "BackgroundTimer"
cordova-plugin-background-mode 0.7.2 "BackgroundMode"
cordova-plugin-device 2.0.2 "Device"
cordova-plugin-foreground-service 1.1.1 "Cordova Foreground Service"
cordova-plugin-mauron85-background-geolocation 3.0.1 "CDVBackgroundGeolocation"
cordova-plugin-whitelist 1.3.3 "Whitelist"

而这个JavaScript代码:

onDeviceReady: function () {
    this.receivedEvent('deviceready');
    cordova.plugins.foregroundService.start('GPS Running', 'Background Service');
    cordova.plugins.backgroundMode.on('activate', function () {
        console.log("Disabled webview optimizations");
        cordova.plugins.backgroundMode.disableWebViewOptimizations();
    });
    cordova.plugins.backgroundMode.enable();
    var socket = io.connect('http://server.com:3000');
    socket.on('request', function (empty) {
        BackgroundGeolocation.getCurrentLocation(function (location) {
            socket.emit('location', JSON.stringify(location));
        });
    });
    BackgroundGeolocation.configure({
        locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
        desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
        notificationTitle: 'Location, Location!',
        notificationText: 'enabled',
        debug: false,
        interval: 30 * 1000,
        fastestInterval: 30 * 1000,
        activitiesInterval: 30 * 1000
    });
    BackgroundGeolocation.on('location', function (location) {
        // handle your locations here
        // to perform long running operation on iOS
        // you need to create background task
        //console.log("regular location:");
        //console.log(location);
        BackgroundGeolocation.startTask(function (taskKey) {
            // execute long running task
            // eg. ajax post location
            // IMPORTANT: task has to be ended by endTask
            BackgroundGeolocation.endTask(taskKey);
        });
    });
    BackgroundGeolocation.on('stationary', function (stationaryLocation) {
        // handle stationary locations here
        //console.log("Stationary object");
        //console.log(stationaryLocation);
    });
    BackgroundGeolocation.on('error', function (error) {
        console.log('[ERROR] BackgroundGeolocation error:', error.code, error.message);
    });
    BackgroundGeolocation.on('start', function () {
        console.log('[INFO] BackgroundGeolocation service has been started');
    });
    BackgroundGeolocation.on('stop', function () {
        console.log('[INFO] BackgroundGeolocation service has been stopped');
    });
    BackgroundGeolocation.on('authorization', function (status) {
        console.log('[INFO] BackgroundGeolocation authorization status: ' + status);
        if (status !== BackgroundGeolocation.AUTHORIZED) {
            // we need to set delay or otherwise alert may not be shown
            setTimeout(function () {
                var showSettings = confirm('App requires location tracking permission. Would you like to open app settings?');
                if (showSetting) {
                    return BackgroundGeolocation.showAppSettings();
                }
            }, 1000);
        }
    });
    BackgroundGeolocation.on('background', function () {
        console.log('[INFO] App is in background');
        // you can also reconfigure service (changes will be applied immediately)
        BackgroundGeolocation.configure({
            debug: false
        });
    });
    BackgroundGeolocation.on('foreground', function () {
        console.log('[INFO] App is in foreground');
        BackgroundGeolocation.configure({
            debug: false
        });
    });
    BackgroundGeolocation.on('abort_requested', function () {
        console.log('[INFO] Server responded with 285 Updates Not Required');
        // Here we can decide whether we want stop the updates or not.
        // If you've configured the server to return 285, then it means the server does not require further update.
        // So the normal thing to do here would be to `BackgroundGeolocation.stop()`.
        // But you might be counting on it to receive location updates in the UI, so you could just reconfigure and set `url` to null.
    });
    BackgroundGeolocation.on('http_authorization', () => {
        console.log('[INFO] App needs to authorize the http requests');
    });
    BackgroundGeolocation.checkStatus(function (status) {
        console.log('[INFO] BackgroundGeolocation service is running', status.isRunning);
        console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled);
        console.log('[INFO] BackgroundGeolocation auth status: ' + status.authorization);
        // you don't need to check status before start (this is just the example)
        if (!status.isRunning) {
            BackgroundGeolocation.start(); //triggers start on start event
        }
    });
},

并在此应用程序上禁用节能,我成功了。

同样为了确保这个东西有效,我让后端服务器每 60 秒通过无线方式向我发送一个 websocket 数据包。

这很讨厌,但它有效。在某个时候,我将开始削减每个依赖项,以查看所需的最低限度是什么,但这有效!

随着新版本的Android,谷歌越来越多地实现了在后台杀死应用程序的功能。在宏伟的计划中,这是一件好事,这是为了节省电池寿命,但是,这对开发人员来说并不是好消息。具体来说,看看他们的文档:打瞌睡和自适应电池。

撰写本文时,还没有Cordova插件可以解释较新的Android版本的这些功能(doze首先在Android 6.0中引入,后来的版本使其更加苛刻(。

除了编写自己的插件,这里没有好的答案。保持您的应用程序在后台运行是一项功能,随着新版本的 android 发布,该功能确实(并且将(需要持续维护,也就是说,一般来说,这不是 cordova 擅长的。

Cordova作为开发工具不适用于深层原生功能。使用科尔多瓦作为平台,如果您需要为您的主要产品提供快速随附的应用程序。

是的,请求以某种方式兑现并在应用程序恢复中设置 - 呵呵...

试试这个:

如果可以的话,请使用"@mauron85/cordova-plugin-background-geolocation"帖子模板,或者删除url和syncUrl并使用"cordova-plugin-advanced-http"回调方法在onLocation((中发送您自己的http请求,该方法从本机发送请求。

(现在在安卓 7 上为我工作了数周 - 三星银河(

最新更新