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

Background location doesn't work in Android >= 10 (API level 29) #19

Closed
RaddishIoW opened this issue Apr 26, 2021 · 24 comments
Closed

Background location doesn't work in Android >= 10 (API level 29) #19

RaddishIoW opened this issue Apr 26, 2021 · 24 comments
Assignees
Labels
android Related to the Android platform wontfix This will not be worked on

Comments

@RaddishIoW
Copy link

Describe the bug
Background location updates are blocked on Android >=10 (API level 29) due to missing permissions.

Expected behavior
The plugin should include the permissions necessary to enable background location tracking on Android >=10 (API level 29).

According to the docs, the plugin must include the ACCESS_BACKGROUND_LOCATION permission.

@RaddishIoW RaddishIoW self-assigned this Apr 26, 2021
@RaddishIoW RaddishIoW added the bug Something isn't working label Apr 26, 2021
RaddishIoW added a commit that referenced this issue Apr 26, 2021
@HarelM
Copy link
Collaborator

HarelM commented Apr 27, 2021

I'm not sure there's an actual issue here. My users have android 10 and 11 and are using this plugging successfully.
Assuming you use foreground mode.
I'd be happy to help if something is not working for you...

RaddishIoW added a commit to HaylLtd/background-geolocation-android that referenced this issue Apr 27, 2021
@RaddishIoW
Copy link
Author

Hmmm... This is a bit confusing!

I've read through the comments on that issue you linked in the PR, and there's really quite a few people seeing issues with this.
I experienced it with one of my users who upgraded to a new phone, which I assume is running Android 10 or 11. The option to allow the app to track location in the background just isn't there any more - the permissions in the Settings do not list "Always" as an option.
Now, if I understand what you're saying correctly: this doesn't matter, as the background/foreground service still runs and reports location data as it's really still in the foreground. Is that right?

@HarelM
Copy link
Collaborator

HarelM commented Apr 27, 2021

Yes.
Foreground service = a service that run, has a notification icon but still works when the screen is locked, the app is minimized or the app is in the front.
Background service = the same as above without letting the user know - i.e. without the notification icon.
Background service requires the permission you added but also requires a store approval by google.
This is what the approval dialog with the "always" option is there for - allow the user know the app can collect location data all the time, not just when the app is "running".
Since I want the user to know the app is running, foreground is the right approach I believe for my case, I guess it's also the case in most apps.
Supporting both modes (foreground and background) is possible but I think it's not easy to implement as you need to do some work in the post install script in the Cordova side (or at least this is how it was done in the plugin that you can pay for - i.e. transistorsoft).
I think we should add support only for foreground and allow the community to support this if someone needs it by submitting a pull request later on.

@RaddishIoW RaddishIoW added the android Related to the Android platform label May 3, 2021
@RaddishIoW
Copy link
Author

I've been doing some testing with my app and the plugin on Android, and it's all over the place. I'm still looking at it, and have still to fully understand it, but I did find this in the Android docs here:

Caution: If your app starts a foreground service while running in the background on a device that runs Android 11 (API level 30) or higher, your app cannot access location information unless the user has granted the ACCESS_BACKGROUND_LOCATION permission to your app. For more information, view the guidance about the while-in-use restrictions that are associated with foreground services.

From what I can understand, unless startForeground is true, the plugin does indeed start a foreground service when the app is in the background. Am I correct in thinking that?

@HarelM
Copy link
Collaborator

HarelM commented May 9, 2021

If I understood your question then yes. You have to set stratForground to true so that the app will start a service in foreground mode to be able to collect location while the app in not in the front. When clicking a button to start this service you make sure that the service is created when the app is in the foreground and avoid the above pitfall.

@francescognarra
Copy link

Hello all,
I have an app with this installed plugin (from mauron85 repo) and I have following configuration:

const config: BackgroundGeolocationConfig = {
desiredAccuracy: 20,
stationaryRadius: 10,
distanceFilter: 20,
startForeground: true,
interval: 5000,
fastestInterval: 2000,
activitiesInterval: 5000,
// startForeground: true,
notificationsEnabled: true,
notificationTitle: "Tracciamento posizione",
notificationText: "ATTIVA",
debug: true,
url: 'http://localhost:3000/api/v1/trips/test',
httpHeaders: {
// 'X-FOO': 'bar'
},
// customize post properties
postTemplate: {
latitude: '@latitude',
longitude: '@longitude'
},
stopOnTerminate: false // enable this to clear background location settings when the app terminates
};
this.backgroundGeolocation.configure(config).then(() => {

  this.backgroundGeolocation
    .on(BackgroundGeolocationEvents.background)
    .subscribe((data: BackgroundGeolocationResponse) => {

      this.bgWork(data);
    });

  this.backgroundGeolocation
    .on(BackgroundGeolocationEvents.foreground)
    .subscribe((data: BackgroundGeolocationResponse) => {

      this.bgWork(data);
    });

  this.backgroundGeolocation
    .on(BackgroundGeolocationEvents.location)
    .subscribe((data: BackgroundGeolocationResponse) => {

      this.bgWork(data);

    });

});

bgWork function is where I send data to server.

The app must send location data to a server backend even if is in background but it seems doesn't work for me. I see notification for foreground service but how can I send data when app is in background?

Can you help me?

@HarelM
Copy link
Collaborator

HarelM commented May 26, 2021

This won't work in general as explained in this thread.
You need to use the plugin's http native calls to send data, this is the only thing that will work in the background as you want.
The other option, which is what I'm doing is waiting for the app to get to the front and send all that data that was missed when the app was in the background.
This is a Cordova limitation as I have explained in this thread.

@francescognarra
Copy link

This won't work in general as explained in this thread.
You need to use the plugin's http native calls to send data, this is the only thing that will work in the background as you want.
The other option, which is what I'm doing is waiting for the app to get to the front and send all that data that was missed when the app was in the background.
This is a Cordova limitation as I have explained in this thread.

Is the following configuration correct to send data through plugin's http native calls?

const config: BackgroundGeolocationConfig = {
desiredAccuracy: 20,
stationaryRadius: 10,
distanceFilter: 20,
startForeground: true,
interval: 5000,
fastestInterval: 2000,
activitiesInterval: 5000,
// startForeground: true,
notificationsEnabled: true,
notificationTitle: "Tracciamento posizione",
notificationText: "ATTIVA",
debug: true,
url: 'http://192.168.1.130:3000/api/v1/trips/test',
httpHeaders: {
// 'X-FOO': 'bar'
},
// customize post properties
postTemplate: {
latitude: '@latitude',
longitude: '@longitude'
},

stopOnTerminate: false // enable this to clear background location settings when the app terminates
};
Do I have to add something else or is this correct?

@HarelM
Copy link
Collaborator

HarelM commented May 26, 2021

I'm not using the url part of the plugin, but I guess it should work.
Try it out on your server and let us know.
@RaddishIoW can we close this issue?

@francescognarra
Copy link

@HarelM I have tried but when app is active it calls the url http://192.168.1.130:3000/api/v1/trips/test but when in background it does nothing. If BACKGROUND_GEOLOCATION permission isn't added how can the app locate you position and so send it to backend?

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2021

@francescognarra there's a misconception about the definition of background and foreground and this permission.
Try to read again though this thread again including the linked commits and PRs to better understand this.
The bottom line is that in most cases I don't think this permission is needed...

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2021

Is your plugin defined as foregroundService?

@francescognarra
Copy link

@HarelM another question: I'm using original plugin from mauron85/cordova-plugin-background-geolocation. Should I use this HaylLtd/cordova-background-geolocation-plugin and, if so, how can I point to this?

@francescognarra
Copy link

Is your plugin defined as foregroundService?

How can I check this?

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2021

On the configuring of the plugin you can specify to use foreground service (configure method).
In order to migrate to this plugin you simply need to uninstall the existing one and install this one using cordova (see our readme).
You need this plugin I order to work with Android 10, this might explain the problem you face...

@francescognarra
Copy link

@HarelM I have installed your plugin and it seems to work :D
I just noticed that geolocation icon, after some minutes of inactivity, will turn off in foreground service. Is it normal?

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2021

You need to make sure the app doesn't get killed by the OS.
The best way to do it is in the app configuring.
I use a popup in my app to tell users they need to do it when they start recording a route.
As far as I know this can't be done programmatically...

@RaddishIoW
Copy link
Author

@HarelM Sorry for the delay! Yep, I'll close this as won't fix. I'll endeavour to update the docs ASAP to make it absolutely clear why we don't need this permission, and make it clearer how to post updates in the background on Android.

@RaddishIoW RaddishIoW added wontfix This will not be worked on and removed bug Something isn't working labels Jun 2, 2021
@phiasco12
Copy link

Is your plugin defined as foregroundService?

still not sure where that goes!

@HarelM
Copy link
Collaborator

HarelM commented Nov 8, 2021

@phiasco12
Copy link

phiasco12 commented Nov 8, 2021

startForeground

My app seems to be working fine in the background and foreground without the startForeground though!!

am I missing here?

@HarelM
Copy link
Collaborator

HarelM commented Nov 8, 2021

It will probably stop after a while if you won't assign it to start as a foreground service...
Please try and read more of the documentation...

@calmstrom
Copy link

This won't work in general as explained in this thread. You need to use the plugin's http native calls to send data, this is the only thing that will work in the background as you want. The other option, which is what I'm doing is waiting for the app to get to the front and send all that data that was missed when the app was in the background. This is a Cordova limitation as I have explained in this thread.

Hi @HarelM ,
I'm trying something similar and the app works perfectly fine when in foreground and when device is unlocked. However, I see that I lose the data when the app moves to background / device is locked although I have used the foreground mode. Could you please post a snippet of your code sample of sending all the data that was missed when the app was in the background for reference.

@HarelM
Copy link
Collaborator

HarelM commented Jan 18, 2022

The following is the code I use in my app:
https://github.com/IsraelHikingMap/Site/blob/main/IsraelHiking.Web/src/application/services/geo-location.service.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Related to the Android platform wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants