seididieci / capacitor-backround-geolocation

Ionic capacitor backgroung geolocation Plugin
MIT License
12 stars 2 forks source link

I can't manage to keep recording GPS location afther the app is moved to the background #14

Closed iec989 closed 3 years ago

iec989 commented 3 years ago

The location is working perfectly fine as soon as the app is open, but when I swith to another app, or as soon as I "minimize" it, I stop recording the location.

I thought the logic in addBackgroundListener should allow me to achieve that, but it doesn't seem to be the case.

Am I missing anything, or it is simply not possible to keep tracking location once the app gets closed with Capacitor?

        initGeoLocation({ dispatch, commit, state }){
            var { BackgroundGeolocation }  = Plugins;
            commit('setGeolocation',BackgroundGeolocation);
            state.vars.geolocation.addListener('onLocation', (location) => {
                console.log('Got new location: '+location.latitude + ', '+location.longitude);
                dispatch('geolocate',{loc:location});
            });
            state.vars.geolocation.initialize({
                notificationText: 'Your app is running, tap to open.',
                notificationTitle: 'App Running',
                updateInteval: 10000,
                requestedAccuracy: BgGeolocationAccuracy.HIGH_ACCURACY,
                smallIcon: 'ic_small_icon',
                startImmediately: false,
            });
        },
        addBackgroundListener({ dispatch, commit, state }){
            const { App, BackgroundTask } = Plugins;
            let user = state.user;
            let geoobj = state.vars.geolocation;
            App.addListener('appStateChange', (state) => {
                if (!state.isActive) {
                    let taskId = BackgroundTask.beforeExit(async () => {
                            geoobj.start();
                        /*BackgroundTask.finish({
                            taskId
                        });*/
                    });
                }
            })
        },
seididieci commented 3 years ago

In my application I still get notifications (inside the onLocation callback) even if I lock the phone and not also when the app is "minimized"... I think that your addBackgroundListner should not be needed to achive this (I haven't something similar in my app). If this can help you, this plugin should keep the whole application live and keep javascript funcions run also when the app get minimized, but I noticed some phone firmware are a bit aggressive in power management (I seen some Huawei devices kill the application when starting the camera from it to take a photo)... So may it be related to somenthing in the OS? What Android version are you running on? Do you notice that the chrome debbuger disconnects when the app is in background?

iec989 commented 3 years ago

Thank you for coming back to me @seididieci despite it is out of the plugin scope!

I have a HUAWEI Mate 20 Pro running Android 10 - but I face the same issue with the Android Emulator (also using Android 10). It happens as soon as I move out of the app, it doesn't even wait a sec!

This is a VueJS project using Capacitor.

RE: the plugins should keep the whole application live - which plugin(s) could help me to achieve that?

SalahAdDin commented 3 years ago

I had a issue related to that, in summary: when you send your app to back ground, this plugin's service is disconnected, when you open the app again, the service is re-connected and works normally.

seididieci commented 3 years ago

Hi, I checked with an emulator with Andoid 11 and can confirm that if you put the app in the background somehow the GPS system icon disapperas like there is no app using GPS at all... I will double check this and put a fix when available...

Thank You for reporting.

seididieci commented 3 years ago

Good news, I found the Issue in a missing permission (needed from SDK 29 and later)... I'm fixing this in PR #15

seididieci commented 3 years ago

Tested on android 11, the new SDK(s) seems to force the user to enable background geolocation only from settings: immagine So we have to handle this informing the users somewhere in the app

ramay2088 commented 3 years ago

Hi I am having same issue I also used this in my android.xml file but still not working

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

It works for almost five minutes then get stoped

seididieci commented 3 years ago

Hi @ramay2088, Do you see some errors/messages in the log? Can you be more specific about "then get stopeed"? Was the application killed by the OS or the notification disappears?

ramay2088 commented 3 years ago

actually according google android 8 + was required to use foreground services for background tracking . i was geeting it wrong .. thanks