zoontek / react-native-permissions

An unified permissions API for React Native on iOS, Android and Windows.
MIT License
4.1k stars 836 forks source link

Android 11 (API 30): `request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION)` never returns `blocked` #776

Closed rarenatoe closed 1 year ago

rarenatoe commented 1 year ago

Bug summary

In my app I run the following code

await requestMultiple([PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION])
const status = await request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION)
console.log({ status })

If by the third time the user denies the permission, it doesn't show the popup anymore (which is expected behavior), but it always returns denied, never blocked.

I am handling this by locally counting the number of attemps, but it would be nice if this were fixed or something told me what I did wrong. 🙏

Library version

3.7.2

Environment info

System:
    OS: macOS 13.3.1
    CPU: (8) arm64 Apple M1
    Memory: 52.34 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 3.5.1 - ~/.nvm/versions/node/v18.15.0/bin/yarn
    npm: 9.6.6 - ~/.nvm/versions/node/v18.15.0/bin/npm
    Watchman: 2023.05.01.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.12.1 - /Users/ren/.rvm/gems/ruby-2.7.6/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4
    Android SDK:
      Android NDK: 22.1.7171670
  IDEs:
    Android Studio: 2022.1 AI-221.6008.13.2211.9619390
    Xcode: 14.3/14E222b - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.13 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.1.0 => 18.1.0 
    react-native: 0.70.9 => 0.70.9 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

  1. Write this, in some function:
    await requestMultiple([PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION])
    const status = await request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION)
    console.log({ status })
  2. give that function to a button.
  3. click the button 3 times and deny them all.
  4. See how the status is always denied.
  5. keep clicking.
  6. status is still denied.

Reproducible sample code

await requestMultiple([PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION])
const status = await request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION)
console.log({ status })
zoontek commented 1 year ago

@rarenatoe Hi, this is unfortunately not a place to get help, but to declare issues with the library. For that, please be sure that it always occurs (by providing a minimal reproduction repository).

You can also check the example app: if it doesn't occur with, that's mean the issue might came from something else.

zoontek commented 1 year ago

PS: ACCESS_BACKGROUND_LOCATION MUST be requested with ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION (documentation)

Replace your sample code with:

const requestLocation = async () => {
  const status = await requestMultiple([
    PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION,
    PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, // or ACCESS_COARSE_LOCATION
  ]);

  console.log({ status });
  return status;
};

Should do the trick.

rarenatoe commented 1 year ago

@zoontek what you proposed just doesn't show the popup. Seems like starting on API Level 30, foreground/background requests have to be made distinctly.

Still, seems like ACCESS_FINE_LOCATION and ACCESS_COARCE_LOCATION return blocked.