Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get locations when in background #126

Open
miistakis opened this issue Oct 23, 2022 · 22 comments
Open

Cannot get locations when in background #126

miistakis opened this issue Oct 23, 2022 · 22 comments

Comments

@miistakis
Copy link

This is on an Andriod os 10, using the 1.1 plugin as the app isnt andriodx enabled.

Everything works great in foreground, but soon as it goes into background it suspends activity, until I bring it back to foreground. Any suggestions on what to try would be appreciated.

@HarelM
Copy link
Collaborator

HarelM commented Oct 23, 2022

Make sure you use startFouregrond and no battery optimization is applied to the app.
If you are not using androidx I would use the mauron85 one.

@miistakis
Copy link
Author

I was using mauron85, but was running into some permission issues, which were fixed when I switched to this one, but its not working in the background.

I assume these are the ones you mean:
startForeground: true,
saveBatteryOnBackground: false,

Thanks

@HarelM
Copy link
Collaborator

HarelM commented Oct 23, 2022

Also battery optimization in the app settings in the OS.
But you'll need to debug using android studio probably...

@miistakis
Copy link
Author

Ok, will check out the power settings in the OS, hadnt thought of that. I have been using android studio, no real stand out issues.

@miistakis
Copy link
Author

Is start and end task required, or just for long IOS tasks?

@miistakis
Copy link
Author

Here is what happens in the log when it goes into background:

D/CordovaActivity: Paused the activity.
I/com.tenforwardconsulting.bgloc.cordova.BackgroundGeolocationPlugin: App will be paused multitasking=true
I/chromium: [INFO:CONSOLE(195)] "[INFO] App is in background", source: file:///android_asset/www/js/app.js (195)
D/com.marianhello.bgloc.service.LocationServiceImpl: Service in [STARTED] state. cmdId: [3]. startId: [2]
D/CordovaActivity: Stopped the activity.
I/com.tenforwardconsulting.bgloc.cordova.BackgroundGeolocationPlugin: App is no longer visible
V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@b5e70b3, this = DecorView@713fe96[MainActivity]

@miistakis
Copy link
Author

@HarelM is there a power setting specific to android? The setting I see in the documentation is for IOS, which I have set as well, but wondering if there is something else specific for android?

I have the app working in background, but it needs to be connected via USB, which makes me think of your power recommendations. The OS settings are to not optimize for battery and to allow battery usage in background, but still soon as I disconnect from power it no longer records points in background.

@HarelM
Copy link
Collaborator

HarelM commented Oct 24, 2022

No, just the OS battery optimizing...

@miistakis
Copy link
Author

@HarelM Are there different settings for background vs sleep? If I just put the app in the background of the phone, say work on a different app, it still works fine, even disconnected from the usb cable. But if I let the device go dark, if it is not plugged into USB power, it stops working, until you plug it in or open the device back up.

@HarelM
Copy link
Collaborator

HarelM commented Oct 25, 2022

I'm not aware of different settings.
I would also consider comparing you other apps and see if the behavior is similar or different.

@mrshlle
Copy link

mrshlle commented Mar 26, 2023

@miistakis , have you found something to fix this issue ? I have the same issue. In foreground, I got the entire ride, in background, it stops and resend location when app is resumed.
On my logs, I have nothing but the last position returned by the plugin while in background mode, and the first returned when app in foreground again.

For information, I have disabled the battery optimization from OS and still not working.
I have tried with @capacitor-community/background-geolocation and I got the entire ride in foreground and background, if this can help to investigate @HarelM

@HarelM
Copy link
Collaborator

HarelM commented Mar 26, 2023

@mrshlle I have a user base of 50k users that uses this plugin and it works as expected.
Moreover, I tried to switch to @capacitor-community/background-geolocation and found it not to work in specific cases.
Make sure you use startForeground in order to get the activity the right priority so it won't get lost.
I also don't listen to event when in background but rather get the last locations from the sqlite db and remove them.
You can see my code here:
https://github.com/IsraelHikingMap/Site/blob/6a721853c4c8b7e7e0f8d24514a6c3b02a61f069/IsraelHiking.Web/src/application/services/geo-location.service.ts
I can assure you the code above is production ready and battle tested.

@miistakis
Copy link
Author

miistakis commented Mar 26, 2023

@mrshlle No, I found the issue was device specific, or maybe that make model (Umidigi A9), and worked on every other device I tested on, so I just released it as is, and have yet to have a complaint.

@mrshlle
Copy link

mrshlle commented Mar 26, 2023

@HarelM yep, startForeground is set to true.
I have tried with your code, and by taking the last location from sqlite, but the problem comes from the locations that are not even stored when in background.
But thanks for your support, I will try more to make it work for my needs.

@miistakis I see, I will test on other devices then, hoping mine is not used a lot.

Thanks for your replies

@weareu
Copy link

weareu commented Jun 29, 2023

I think the issue may be related to mauron85#688 and permissions on new android devices, could that fix be implemented as using this repo does not allow always on some of our devices.

If you think possible I'll give it a go.

@HarelM
Copy link
Collaborator

HarelM commented Jun 29, 2023

I don't have issues in my App, so I can't relate to this issue.
If you would like to debug it and open a PR go ahead.

@MHameed45
Copy link

MHameed45 commented Jul 4, 2023

@HarelM The Sync API is not receiving list of Locations. I have updated the url and syncurl in parameters.
Under demo app -> on start it does give me the location only one time but nothing returning when app is on background/foreground and no data returning back to the sync api.
below are my parameters settings
Uploading image.png…

@mrshlle
Copy link

mrshlle commented Jul 21, 2023

Hi @HarelM, I am trying to use this plugin again because being able to track location when app is killed is really usefull.

For the moment, I use @capacitor-community/background-geolocation plugin.
When I started implementing it, I had a problem that stopped location after a while in background, and the way to resolve it, was this answer.

I know you are not facing this issue, so I just would like to know if you can help me finding the approximate same method, that is called when it's time to pause the plugin. (In the other plugin, you can find it on this file, line 329)
I have found an "onPause" method on "BackgroundGeolocationPlugin" but not sure if it's where I can do something.

@HarelM
Copy link
Collaborator

HarelM commented Jul 21, 2023

Sorry, I can't help unfortunately.
I tried migrating to the community plugin and it wasn't good enough for that reason mainly.
If your happen to find why and fix it I'll be happy to use the capacitor community one instead of this, as this one is just too complicated...
Here's the relevant issue:
capacitor-community/background-geolocation#80

@risinghero
Copy link

risinghero commented Dec 11, 2023

@HarelM Are there different settings for background vs sleep? If I just put the app in the background of the phone, say work on a different app, it still works fine, even disconnected from the usb cable. But if I let the device go dark, if it is not plugged into USB power, it stops working, until you plug it in or open the device back up.

Some Manufacturers have very aggressive battery saving apps, that tend to disable any background app they dont know about. So first of all it is better to check battery saver app (instruction on how to do this depends on manufacturer of the phone).

@risinghero
Copy link

I think the issue may be related to mauron85#688 and permissions on new android devices, could that fix be implemented as using this repo does not allow always on some of our devices.

If you think possible I'll give it a go.

This can be fixed by just adding cordova-plugin-diagnostics plugin to request permissions and by adding required permissions to your config file

@kerituni12
Copy link

My solution is to push a high-priority Firebase message and location permission is always allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants