HaylLtd / cordova-background-geolocation-plugin

Background and foreground geolocation plugin for Cordova.
Apache License 2.0
57 stars 66 forks source link

Plugin not working in background on iOS #155

Closed tsanko95 closed 1 year ago

tsanko95 commented 1 year ago

Hi, i am encountering an issue with the plugin in my application, which prevents it from functioning correctly on background on iOS devices. I have followed the necessary steps to grant the required permissions, such as "Always" permission for location, physical activity permissions, and background update permissions.

image image

When I minimize the app on a real device (iPad iOS 15.7 and iPhone 11 iOS 16.5), the plugin stops working and does not fire the on('Location') event. I have configured the event to generate a log entry locally in text format and also to send an Ajax call to a web service to register the log in my database. However, none of these actions are executed when the app is in the background.

It is important to note that when using the Xcode simulator with a simulated running activity in the city, the plugin works correctly in the background. (iPhone 13 iOS 16.4)

I would appreciate any guidance or solution you can provide to address this issue and ensure that the plugin functions properly in the background on iOS devices. On Android device works perfectly.

Thank you for your assistance and attention to this matter!

tsanko95 commented 1 year ago
    Sorry, I forgot to include the plugin configuration code:

    BackgroundGeolocation.configure({
        locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
        desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
        stationaryRadius: 1,
        distanceFilter: 1,
        debug: false,
        interval: 10000,
        fastestInterval: 5000
    });

   BackgroundGeolocation.on('location', function(location) {

        var eventosAnteriores = $("#eventos-entrada-salida").html();

        var htmlEntradaSalida = "<a href=\"#\" class=\"border bg-theme rounded-s shadow-xl\">\
            <span>" + moment().format("HH:mm:ss") + "</span>\
            <strong>"+location.latitude+ " - "+ location.longitude + "</strong>\
            <u class=\"color-green-dark\">ENTRADA</u>\
            <i class=\"fa fa-sign-in-alt color-green-dark\"></i>\
        </a>";

        $("#eventos-entrada-salida").html(htmlEntradaSalida + eventosAnteriores);

        cordova.plugin.http.sendRequest('url web service log register', {
            method: 'GET',
            serializer: 'json'
        }, function(data) {
        }, function(error) {
        });

        BackgroundGeolocation.startTask(function(taskKey) {

            cordova.plugin.http.sendRequest('url web service log register', {
                method: 'GET',
                serializer: 'json'
            }, function(data) {
            }, function(error) {
            });

            BackgroundGeolocation.endTask(taskKey);
        });
    });
diegoarbelaez commented 1 year ago

Hello @tsanko95 could you finally make it work? I do you have an example? I cannot make it compile in Android. Please let us know. Thanks!

HarelM commented 1 year ago

Might be a configuration issue, it a bug in the code here. The following works for me in iOS: https://github.com/IsraelHikingMap/Site/blob/f5480f43e0f03c104b1fbbffbdcbfe5fa0e74c68/IsraelHiking.Web/src/application/services/geo-location.service.ts

tsanko95 commented 1 year ago

Hi @diegoarbelaez and @HarelM finally i make it work and figured out why the plugin wasn't working for me. It turns out that I had misconfigured it by using "locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER". The "ACTIVITY_PROVIDER" is not designed for background mode, as it clearly states in the documentation.

After some troubleshooting, I found that the correct option to use for background mode is "DISTANCE_FILTER_PROVIDER". This is the best option for background tracking, and once I made this change, the plugin started working perfectly.

So, if you are facing similar issues with the plugin, make sure to use "DISTANCE_FILTER_PROVIDER" for background mode as recommended in the documentation. I hope this helps others who might encounter the same problem.

Thanks to all for your support!

diegoarbelaez commented 1 year ago

Thank you @tsanko95 I'm still having issues compiling the project using the plugin. There are several things are not detailed explained in the implementation of the plugin, at least for beginners. I wonder if you have an already running example which can be used as a base. (IONIC 6 + Angular) If it works for me, I can create a tutorial for other people who want to use the plugin. Please let me know.

Thanks!

ghost commented 1 year ago

@diegoarbelaez If it works for you, I suggest tutorial be with cordova/ionic but without Angular/React libraries but with vanilla javascript, html and css. It would be helpful for beginners.

HarelM commented 1 year ago

I'll be closing this issue now. Feel free to open a PR to update the docs or the readme.