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

Background location doesn't update on Android 11 #42

Closed nemoneph closed 2 years ago

nemoneph commented 2 years ago

Describe the bug Hello, First of all, thanks for all your work on this plugin.

I have a small app, with a background geolocation working fine until I switch to Android 11. After further tests, it's always working on Android 8,9,10 but Android 11 it's doesn't update location when app is in Background.

It's seem link to this isssue / the need of ACCESS_BACKGROUND_LOCATION for Android 11 and above. https://stackoverflow.com/questions/62483266/location-updates-in-android-11

https://developer.android.com/training/location/permissions

Does someone have any clue on this or trick ?

Thanks

diachedelic commented 2 years ago

Hi @nemoneph . According to Google's documentation, the ACCESS_BACKGROUND_LOCATION permission is only required where a location-gathering service is started in the background. This plugin technically only requests "foreground" updates:

The system considers your app to be using foreground location if a feature of your app accesses the device's current location in one of the following situations:

  1. An activity that belongs to your app is visible.
  2. Your app is running a foreground service. When a foreground service is running, the system raises user awareness by showing a persistent notification. Your app retains access when it's placed in the background, such as when the user presses the Home button on their device or turns their device's display off.

Do you see the notification when your app is backgrounded on Android 11?

nemoneph commented 2 years ago

Yes, I see the notification bar when app is in Background.

And what I think (it's a assumption because I'm not an expert on this), it's that with Android 11, the technique with the foreground service which work fine on Android 9,10.. is not enough anymore.

Quote from stackoverflow "Starting with Android 11, a foreground "location" service is no longer enough to get background location updates. We now need to request ACCESS_BACKGROUND_LOCATION, if the foreground service is started in the background (such as a broadcast receiver):"

Maybe someone with Android 11 can confirm / invalid this theory ?

diachedelic commented 2 years ago

I think this is a key point from that StackOverflow post:

if the foreground service is started in the background (such as a broadcast receiver)

In our case, the foreground service is started whilst the app is still in the foreground, so I still don't believe we should need the ACCESS_BACKGROUND_LOCATION permission. I will test this in the next few days on an Android 11 device and get back to you. In the meantime, I have a few questions:

  1. What Android 11 device are you using?
  2. Are you sure you are not being impacted by a "battery saving" feature? See dontkillmyapp.com.
  3. When you select the notification, does it open your app or does it open the settings app?
nemoneph commented 2 years ago

Yes I understand, you are probably right.

  1. My Device is a Samsung Galaxy Note 10+. My App was working when it's was on background on this device with Android 10. After the update to Android 11, doesn't work anymore.

Note : some other apps in the same thematic (tracking running/cycling activity) like Strava work fine, but others made by small editors have the same problem on my device.

  1. Hmm Samsung have bad review indeed .. Tested the dontkillmyapp app and I have a score of 64% (doesn't know what it's really mean). Also, after that, I have excluded my app from battery optimizations. Doesn't change anything.

  2. It open my app.

diachedelic commented 2 years ago

The plugin works on an Oppo Reno Z running Android 11. Unfortunately I do not have another Android 11 device to debug this issue on. Please let me know if you discover a solution.

nemoneph commented 2 years ago

OK, thanks for the feedback. I close the issue, because it's seem links to my phone..

I will add a comment if I find the solution, or re-open if I find more info on my problem.

NikolaStojicic commented 2 years ago

After updating to Android 11, Xiaomi Poco F2 Pro doesnt update location in background (inspecting chrome dev tools, does not show result from native watcher), while Andorid 10, Huawei P30 Pro and Andorid 6 Nexus 5, works just fine.

jamesdiacono commented 2 years ago

Does the notification show when you send the app to the background, @NikolaStojicic ? If so, when you tap the notification, does it take you to the app or the settings?

NikolaStojicic commented 2 years ago

Notification is shown and when tapped it does take me to the settings. It stoped updating location after latest phone update. I switched to this plugin, which works fine for now (cuts off after 30min of phone being totally inactive, screen off, no movement) https://github.com/seididieci/capacitor-backround-geolocation

diachedelic commented 2 years ago

When the notification takes you to the settings rather than the app, that generally means its icon has been misconfigured. Maybe this is causing the problem.

If anyone can discover why seididieci's plugin works but this one doesn't, I will implement a fix. The two plugins appear to use almost identical mechanisms.

shipley-dcc commented 2 years ago

I thought I should add my experience here in case it helps anyone. I had a similar experience with some users expressing issues with my app tracking location in the background on Android 11. But I tested it on BrowserStack with a Samsung Galaxy S21 and a Google Pixel 5 and the app worked fine on both devices with Android 11. However, I got hold of a Nokia 1.4 with Android Go Edition version 11 and the app did stop tracking in the background. The fix for this was to add the ACCESS_BACKGROUND_LOCATION permission to my app. I now request the usual COARSE and FINE permissions and then if the app is running on an Android device with an OS version greater than 10 I then ask for the BACKGROUND permission which opens the Settings app and the user has to set it manually. This contradicts the Android Developer documentation, but there you go.

diachedelic commented 2 years ago

Thanks Dave. Weirdly, the fix for seididieci's plugin actually removes that permission! So I am still a bit confused. @NikolaStojicic , do you request the ACCESS_BACKGROUND_LOCATION permission in AndroidManifest.xml?

shipley-dcc commented 2 years ago

You can't ask for all three permissions at once, you have to ask for foreground permission (COARSE and/or FINE), then ask for BACKGROUND permission in a separate request. So maybe Seididieci's plugin was running into that issue.

NikolaStojicic commented 2 years ago

@diachedelic I did request ACCESS_BACKGROUND_LOCATION in AndroidManifest.xml. The app I m creating won't be published on Play Store, so I didn't test if it would make difference without it.

diachedelic commented 2 years ago

I suspect that declaring the permission in the manifest is different from requesting it at runtime.

However, I got hold of a Nokia 1.4 with Android Go Edition version 11 and the app did stop tracking in the background.

It occurs to me that the OS might be preventing your app from running in the background. Have you investigated https://dontkillmyapp.com?

ahmadmasoum commented 2 years ago

I have the same issue on Samsung a51 android 11 background location works for 5 minutes then stops until you open the app again

Side note: I'm also using Background Mode plugin in order to receive signalR updates from the server and it works even after 30mins (Both plugins are used on the same app and the same phone).

diachedelic commented 2 years ago

@ahmadmasoum Do you see the notification when the app is moved to the background? What happens when you tap the notification - does it open the app or the settings?

ahmadmasoum commented 2 years ago

@ahmadmasoum Do you see the notification when the app is moved to the background? What happens when you tap the notification - does it open the app or the settings?

Yes, it opens the settings page

diachedelic commented 2 years ago

@ahmadmasoum In my experience, that means the notification icon has been misconfigured. Perhaps it is also affecting the emission of location updates. If you have configured an icon in AndroidManifest.xml, can you remove it and see if that a) fixes the notification tap behaviour, and b) fixes your background locations?

ahmadmasoum commented 2 years ago

It turns out it wasn't notification configuration problem Even though i disabled optimization from settings i had to call

        this.backgroundMode.disableBatteryOptimizations();
        this.backgroundMode.disableWebViewOptimizations()

when background mode is active. List this

      this.activateSubscription = this.backgroundMode.on('activate').subscribe(() => {
        this.backgroundMode.disableBatteryOptimizations();
        this.backgroundMode.disableWebViewOptimizations();
      });

This is the background mode plugin im using

Note: the disableBatteryOptimizations(); and disableWebViewOptimizations() must be called when the background mode is active otherwise the app will stop after 5 minutes

diachedelic commented 2 years ago

@ahmadmsoum Thanks for the valuable workaround. For those interested in how those two methods work, here are their implementations. It appears that Android supports a REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission.