transistorsoft / flutter_background_geolocation

Sophisticated, battery-conscious background-geolocation & geofencing with motion-detection
https://www.transistorsoft.com/shop/products/flutter-background-geolocation
Other
622 stars 226 forks source link

Location request when always requested but while in use permitted #1050

Closed xunreal75 closed 2 weeks ago

xunreal75 commented 1 year ago

Android 12 has following behavior

when authorization is permitted to while in use, switch App to Background come back from Background after 30 sec Always permission will requested again.

disabling is not working

locationAuthorizationRequest tells the plugin the mode it expects to have been authorized with by the user. Defaults to Always. If you don't care what the user authorizes, you may configure locationAuthorizationRequest: "Any". If you configure locationAuthorizationRequest: 'Always' but the user authorizes only When in Use , the plugin will detect this and show the locationAuthorizationAlert dialog (see disableLocationAuthorizationAlert to disable this behaviour).

(base) larshuth@MacBook-Pro-von-Lars bladenightapp-flutter_v2 % flutter doctor -v [✓] Flutter (Channel stable, 3.7.12, on macOS 13.3.1 22E772610a darwin-arm64, locale de-DE) • Flutter version 3.7.12 on channel stable at /Users/larshuth/development/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 4d9e56e694 (vor 5 Wochen), 2023-04-17 21:47:46 -0400 • Engine revision 1a65d409c7 • Dart version 2.19.6 • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2) • Android SDK at /Users/larshuth/Library/Android/sdk • Platform android-33, build-tools 33.0.2 • ANDROID_SDK_ROOT = /Users/larshuth/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14E222b • CocoaPods version 1.12.0

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.2) • IntelliJ at /Users/larshuth/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app • Flutter plugin version 73.1.1 • Dart plugin version 231.9065

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.1) • IntelliJ at /Users/larshuth/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/231.8770.65/IntelliJ IDEA.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.2) • IntelliJ at /Users/larshuth/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/231.9011.34/IntelliJ IDEA.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] Connected device (4 available) • M2103K19G (mobile) • mrcuciq47dzl8xbm • android-arm64 • Android 13 (API 33) • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 12 (API 32) (emulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.3.1 22E772610a darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 113.0.5672.126

[✓] HTTP Host Availability • All required HTTP hosts are available

publish_to: 'none' version: 1.0.2347

environment: sdk: ">=2.17.0 <3.0.0"

dependencies: asset_fill: ^1.0.2 barcode_widget: ^2.0.2 cached_network_image: ^3.2.3 convert: ^3.1.1 crypto: ^3.0.2 cupertino_icons: ^1.0.4 dart_mappable: ^1.1.1 device_info_plus: ^8.1.0 encrypt: ^5.0.1

f_logs: ^2.0.1

f_logs: path: ../Flogs/ fast_color_picker: ^0.1.1 firebase_core: ^2.9.0 firebase_crashlytics: ^3.1.0 flutter: sdk: flutter flutter_background_geolocation: ^4.11.0 flutter_cache_manager: ^3.3.0 flutter_device_type: ^0.4.0 flutter_local_notifications: ^13.0.0 flutter_localization: ^0.1.10 flutter_map: ^4.0.0 flutter_map_marker_popup: ^4.0.1 flutter_platform_alert: ^0.4.0 flutter_riverpod: ^2.0.2 fluttertoast: ^8.0.9 geo: ^0.3.0 get: ^4.6.5 hive: ^2.2.3 hive_flutter: ^1.1.0 internet_connection_checker: ^1.0.0+1 intl: ^0.17.0 intro_slider: ^4.2.0 latlong2: ^0.8.1

location: ^4.4.0

location:

git:

url: https://github.com/Lyokone/flutterlocation.git

path: packages/location

version: ^5.0.0

modal_bottom_sheet: ^2.1.2

modal_bottom_sheet: git: url: https://github.com/followthemoney1/modal_bottom_sheet.git ref: main path: modal_bottom_sheet onesignal_flutter: ^3.4.1

package_info_plus: ^1.4.2

package_info_plus: ^3.1.2 path_provider: ^2.0.14 permission_handler: ^10.2.0 popover: ^0.2.6+3 riverpod_context: ^0.3.0 rxdart: ^0.27.4 settings_ui: ^2.0.2 share_plus: ^6.3.1 shared_preferences: ^2.1.0 step_progress_indicator: ^1.0.2 test: ^1.22.0

uni_links: ^0.5.1

uni_links2: path: ../uni_links/uni_links/ universal_io: ^2.2.0 url_launcher: ^6.1.10 wakelock: ^0.6.2 web_socket_channel: ^2.3.0 webview_flutter: ^4.0.7

dev_dependencies: build_runner: ^2.1.8 dart_mappable_builder: ^1.1.1 flutter_lints: ^2.0.1 flutter_test: sdk: flutter flutter: generate: false uses-material-design: true

When app come back from Background there is always the request for Always permission but it is disabled At the moment ist happens only on Android

bg.BackgroundGeolocation.onLocation(_onLocation, _onLocationError); bg.BackgroundGeolocation.onMotionChange(_onMotionChange); bg.BackgroundGeolocation.onActivityChange(_onActivityChange); bg.BackgroundGeolocation.onHeartbeat(_onHeartBeat); bg.BackgroundGeolocation.onProviderChange(_onProviderChange);

  var isMotionDetectionDisabled = HiveSettingsDB.isMotionDetectionDisabled;
  var bgLogLevel = HiveSettingsDB.getBackgroundLocationLogLevel;

  if (Platform.isAndroid) {
    return bg.BackgroundGeolocation.ready(bg.Config(
      locationAuthorizationRequest: 'Always',
      reset: true,
      debug: false,
      //ALL
      desiredAccuracy: bg.Config.DESIRED_ACCURACY_NAVIGATION,
      allowIdenticalLocations: true,
      distanceFilter: 0.1,
      heartbeatInterval: 60,
      disableMotionActivityUpdates: isMotionDetectionDisabled,
      locationUpdateInterval: 2500,
      logMaxDays: 1,
      stopAfterElapsedMinutes: 3600,
      preventSuspend: true,
      stopOnTerminate: true,
      startOnBoot: false,
      //enableHeadless: false,
      logLevel: bgLogLevel,
      //request Always permissions
      backgroundPermissionRationale: bg.PermissionRationale(
          title: Localize.current.requestAlwaysPermissionTitle,
          message: Localize.current.noBackgroundlocationLeaveAppOpen,
          positiveAction: Localize.current.openOperatingSystemSettings,
          negativeAction: Localize.current.cancel),
    )).then((bg.State state) {
      _isTracking = state.enabled;
      _isMoving = state.isMoving ?? false;
      notifyListeners();
      return state;
    });
  }
  if (Platform.isIOS) {
    return bg.BackgroundGeolocation.ready(bg.Config(
      locationAuthorizationRequest: 'Always',
      reset: true,
      debug: false,
      //ALL
      logLevel: bgLogLevel,
      desiredAccuracy: bg.Config.DESIRED_ACCURACY_NAVIGATION,
      distanceFilter: 0.1,
      disableMotionActivityUpdates: isMotionDetectionDisabled,
      heartbeatInterval: 60,
      stopDetectionDelay: 5000,
      persistMode: Config.PERSIST_MODE_NONE,
      // Activity Recognition
      activityRecognitionInterval: 3000,
      allowIdenticalLocations: true,
      showsBackgroundLocationIndicator: true,
      preventSuspend: true,
      //request Always permissions
      backgroundPermissionRationale: bg.PermissionRationale(
          title: Localize.current.requestAlwaysPermissionTitle,
          message: Localize.current.noBackgroundlocationLeaveAppOpen,
          positiveAction: Localize.current.openOperatingSystemSettings,
          negativeAction: Localize.current.cancel),
      //iOS only
      locationAuthorizationAlert: {
        'titleWhenNotEnabled':
            Localize.current.noLocationPermissionGrantedAlertTitle,
        'titleWhenOff': Localize.current.locationServiceOff,
        'instructions': Localize.current.enableAlwaysLocationInfotext,
        'cancelButton': Localize.current.cancel,
        'settingsButton': Localize.current.setOpenSystemSettings
      },
      notification: bg.Notification(
          title: Localize.current.bgNotificationTitle,
          text: Localize.current.bgNotificationText,
          smallIcon: 'drawable/ic_bg_service_small'),
    )).then((bg.State state) {
      _isTracking = state.enabled;
      _isMoving = state.isMoving ?? false;
      notifyListeners();
      return state;
    });
  }

_onProviderChange(bg.ProviderChangeEvent event) { FLog.info(text: 'onProviderChangeEvent $event'); switch (event.status) { case ProviderChangeEvent.AUTHORIZATION_STATUS_NOT_DETERMINED: _gpsLocationPermissionsStatus = LocationPermissionStatus.notDetermined; break; case ProviderChangeEvent.AUTHORIZATION_STATUS_RESTRICTED: _gpsLocationPermissionsStatus = LocationPermissionStatus.restricted; break; case ProviderChangeEvent.AUTHORIZATION_STATUS_DENIED: _gpsLocationPermissionsStatus = LocationPermissionStatus.denied; _stopTracking(); break; case ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS: _gpsLocationPermissionsStatus = LocationPermissionStatus.always; break; case ProviderChangeEvent.AUTHORIZATION_STATUS_WHEN_IN_USE: _gpsLocationPermissionsStatus = LocationPermissionStatus.whenInUse; bg.BackgroundGeolocation.ready( bg.Config(disableLocationAuthorizationAlert: true)); break; }

• No issues found!

christocracy commented 1 year ago

See API docs Config.locationAuthorizationRequest. Read what it says for the option "Any".

xunreal75 commented 1 year ago

Please look at my source

it’s disabled by following snippet.

WHEN_IN_USE: _gpsLocationPermissionsStatus = LocationPermissionStatus.whenInUse; bg.BackgroundGeolocation.ready( bg.Config(disableLocationAuthorizationAlert: true));

I tried the same before startlocation without success. After switch display off and on always the location request reappear what’s really distracting and disturbing. Any is a workaround. After App restart it is recommend to show the disclosure, but only once or after providerchange.

christocracy commented 1 year ago

Do this:

locationAuthorizationRequest: 'Any'

xunreal75 commented 1 year ago

Hi, that works. I see that as workaround. At the Moment I rerequest manually after App restart with permissions-package the Always use. I'ts disturbing if anyone forget to test it in real.

christocracy commented 1 year ago

I see that as workaround

I don’t see it that way at all. “Any” means prefer “Always” but don’t bug the user if they select “WhenInUse”.

I'ts disturbing if anyone forget to test it in real.

I don’t understand what you think is disturbing.

github-actions[bot] commented 4 weeks ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been inactive for 14 days since being marked as stale.