capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
188 stars 57 forks source link

There should be permission Allow All time #58

Closed ramay2088 closed 2 years ago

ramay2088 commented 2 years ago

Describe the bug I have to give allow all time permission for android to keep it working in bacckground. but this plugin allow only while using app. How i can overcome this issue. If I give while using app permssion it is not working in background

To Reproduce Steps to reproduce the behavior: just put application in background on call http request on location change

Expected behavior it should work all time while in bacgkround or minimize

Smartphone (please complete the following information):

diachedelic commented 2 years ago

You may be experiencing #14, whereby HTTP requests made from the WebView are throttled in the background.

DanaStefanoska commented 2 years ago

I tried this plugin and it's not #14, I've also added that http. Once I manually go to app info and set the permissions to always, I start getting locations when app is in the background and it's sent just fine. The problem that the OP wants to say is when we request permissions the only 3 choices are "Allow only while using app", "Ask every time" and "Don't allow", the missing one that we need is "Allow all the time".

I tried updating permissions to have ACCESS_BACKGROUND_LOCATION but it still didn't ask for it when asking for permissions. Note: Protection level is dangerous for this permission so that might be the problem.

diachedelic commented 2 years ago

Which versions of Android do you see this problem on?

DanaStefanoska commented 2 years ago

I'm testing on Pixel 6, Android version 12

diachedelic commented 2 years ago

If anyone discovers a solution please let me know.

klantz81 commented 2 years ago

I was confused about this as well, but I found this text at https://developer.android.com/training/location/permissions

On Android 11 (API level 30) and higher, however, the system dialog doesn't include the Allow all the time option. Instead, users must enable background location on a settings page, as shown in figure 7.

klantz81 commented 2 years ago

It looks like this plugin is using a foreground service to access the location rather than require the ACCESS_BACKGROUND_LOCATION permission, so you don't have the option to allow all the time. As long as the app is running even in the background, the foreground service should continue to access the location without the ACCESS_BACKGROUND_LOCATION permission, correct?

Edit: I found some more information here on when ACCESS_BACKGROUND_LOCATION is required if using a foreground service: https://developer.android.com/guide/components/foreground-services

Another edit: According to the documentation you should only need ACCESS_BACKGROUND_LOCATION if the foreground service is started by an app running in the background, but I'm not having any luck without adding location access all the time.

diachedelic commented 2 years ago

At least on Android 11, the "Allow only while using app" permission is sufficient to provide both foreground and background location updates. The confusion arises from the wording of the permission dialog. This wording is especially confusing if you are familiar with the permissions dialog on iOS, which uses similar wording but for a different permissions model. It is crucial to understand what is meant by "background" on both platforms.

As far as Android is concerned, so long as a persistent notification is visible then the user is actively using the app, even if the app itself is not visible. It remains in the foreground in some sense. Android's permissions dialog gives the user three options:

Selecting either of the first two will be sufficient for granting "background" location updates.

The ACCESS_BACKGROUND_LOCATION permission (which this plugin does not use) is more powerful. It allows the app to access the device's location at any time, without informing the user. That is why Android is so reluctant to grant it.

iOS, on the other hand, uses a more intuitive permissions model. It considers the app to be backgrounded if it is not visible on the screen. Before an app can receive background locations updates, the user must choose from the following:

Choosing "Allow while using the app" will result in foreground location updates only. The next time the user sends the app to the background, they will be prompted to upgrade the permission to include background updates. The "Allow once" option provides both foreground and background location updates, but only until the app is killed.

klantz81 commented 2 years ago

Hey.. I just wanted to follow up because the others may be having the same issue I was. In the device log I was seeing this message: Foreground service started from background can not have location/camera/microphone access

Which is why the ACCESS_BACKGROUND_LOCATION was being required. It seems by the time the onActivityStopped() method is called, the app is already in the background, so the isLocationAvailable() method was returning false. Once I removed the onActivityStarted() and onActivityStopped() methods and started the foreground service when the watcher is created in addWatcher(), I'm starting to get consistent location updates.

diachedelic commented 2 years ago

What device and OS are you using @klantz81 ? When you start the foreground service I suppose the notification is presented even whilst the app is visible?

diachedelic commented 2 years ago

If you rename the handleOnStop method to handleOnPause, is the foreground service able to start?

klantz81 commented 2 years ago

I'm using a pixel 6 pro with android 12. Yes, when I start the foreground service in the LocalBinder::addWatcher method, the notification is presented while the app is visible.

If I change the plugin methods to handleOnPause/handleOnResume, the foreground service is able to provide location updates when the app is in the background, and the notification is not presented until I put the app in the background.

diachedelic commented 2 years ago

Thanks @klantz81 . This fix is released in v1.2.1 and v0.3.16, please test.

klantz81 commented 2 years ago

I've checked v1.2.1, and it's working fine. Thank you!

jadsy2107 commented 1 year ago

request permissions on ios does not work after the initial request is responded with 'Don't Allow'

`  async requestLocPermissions() {
    const request = await Geolocation.requestPermissions();
    console.log(request);
  }`

native Geolocation.requestPermissions(#ID) result Geolocation.requestPermissions(#ID) (not false) {coarseLocation: "denied", location: "denied"}

No iOS native pop up asking for permissions, (allow, dont allow) Maybe ios has no way of allowing the permissions after they are denied, closing the app reopening doesnt help you have to delete the app to request permissions again

diachedelic commented 1 year ago

I think you are using a different plugin, @jadsy2107 . See https://github.com/ionic-team/capacitor-plugins/tree/main/geolocation. However, your problems might be solved by swapping to this plugin.

blinkdagger182 commented 1 year ago

include this in your build AndroidManifest.xml file

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

inside the manifest tag and you should be good to go

jongbonga commented 1 year ago

include this in your build AndroidManifest.xml file

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

inside the manifest tag and you should be good to go

I have had this on my Manifest for the longest but it doesn't work, Sentry always report this issue

ACCESS_BACKGROUND_LOCATION: not_granted, 
ACCESS_COARSE_LOCATION: granted, 
ACCESS_FINE_LOCATION: granted, 
ACCESS_NETWORK_STATE: granted