transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.67k stars 425 forks source link

Disabling alert asking for "always" permission when using getCurrentPosition only #313

Closed cosmith closed 7 years ago

cosmith commented 7 years ago

Your Environment

Info.plist:

    <key>NSLocationAlwaysUsageDescription</key>
    <string>We need access to your position to allow shipment tracking.</string>
    <key>NSMotionUsageDescription</key>
    <string>Accelerometer use increases battery efficiency</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>We need access to your position to allow shipment tracking.</string>

Expected Behavior

Calling getCurrentPosition shouldn't ask for "Always" permission.

Actual Behavior

Calling getCurrentPosition asks for "Always" permission with a dialog that we can't disable or customize.

Steps to Reproduce

  1. Answer the location prompt with "When in use"
  2. Ask for current position via getCurrentPosition

Context

Hi, we're using this plugin to get the current location of the user when he is using the app since react native's default method is pretty buggy. We will also use it in background in the future, but right now just when the app is in use.

When we ask for the current position with getCurrentPosition on iOS 11, the user is prompted with this dialog:

image

Related issue: https://github.com/transistorsoft/react-native-background-geolocation/issues/227 (not the same since we don't use start)

We use react-native-background-fetch separately to send data in the background.

Debug logs

christocracy commented 7 years ago

It's all in the docs:

christocracy commented 7 years ago

You can listen to providerchange event to learn which mode the user authorized.

Set locationAuthorizationRequest accordingly, to let the plugin know which mode it should expect to see granted when it requests a location.

cosmith commented 7 years ago

Thanks! We'll read the docs better next time :)

SamMatthewsIsACommonName commented 7 years ago

Hi, Is there a way to disable this prompt entirely? We have a situation where we choose which of two methods we're using, based on whether the user has chosen Always or When in Use (We have uses for each, we grab a couple of instantaneous positions where the app will always be in use, but only do background tracking obviously if this is enabled). So this means either option can work for us.

What you mention about setting the request dynamically, I don't think that works in our case because presumably providerchange only happens on a CHANGE (at least so far in my testing this has been the case), whereas we check permissions on load using react-native-permissions library and update our state to react accordingly. We also use this library to prompt the user for the permissions because we can then do it in a better place for UX.

If we could disable this prompt entirely that would be great thanks.

christocracy commented 7 years ago

Yes, listen to providerchange event and execute setConfig({locationAuthroizationRequest: yourDesiredRequest});

locationAuthorizationRequest tells the plugin which state to expect. You need to change the plugin's expectation.

SamMatthewsIsACommonName commented 7 years ago

Thanks. My providerChange event never seems to fire though unless there is a CHANGE (which makes sense), so how can this config be persisted across multiple sessions? E.g next session there wouldn't be a change, so this wouldn't be called?

For us it makes more sense to actively query the status and then set the config based off of that

christocracy commented 7 years ago

providerchange is always fired right after #configure is executed.

Make sure you listen to providerchange before #configure, so you don’t miss the event.

On Fri, Oct 27, 2017 at 2:29 PM Sam Matthews notifications@github.com wrote:

Thanks. My providerChange event never seems to fire though unless there is a CHANGE (which makes sense), so how can this config be persisted across multiple sessions? E.g next session there wouldn't be a change, so this wouldn't be called?

For us it makes more sense to actively query the status and then set the config based off of that

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/transistorsoft/react-native-background-geolocation/issues/313#issuecomment-340049671, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6l55RJxBJUlFQ0teheLp4wLdM2wsnks5swiERgaJpZM4PCrl4 .

-- Snet form Gmail Mobile

christocracy commented 7 years ago

And be aware the plugin cannot automatically start tracking in background with WhenInUse.

The plugin basically won’t work in the background.

On Fri, Oct 27, 2017 at 2:31 PM Chris Scott christocracy@gmail.com wrote:

providerchange is always fired right after #configure is executed.

Make sure you listen to providerchange before #configure, so you don’t miss the event.

On Fri, Oct 27, 2017 at 2:29 PM Sam Matthews notifications@github.com wrote:

Thanks. My providerChange event never seems to fire though unless there is a CHANGE (which makes sense), so how can this config be persisted across multiple sessions? E.g next session there wouldn't be a change, so this wouldn't be called?

For us it makes more sense to actively query the status and then set the config based off of that

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/transistorsoft/react-native-background-geolocation/issues/313#issuecomment-340049671, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6l55RJxBJUlFQ0teheLp4wLdM2wsnks5swiERgaJpZM4PCrl4 .

-- Snet form Gmail Mobile

-- Snet form Gmail Mobile

SamMatthewsIsACommonName commented 7 years ago

thanks, hmm I'm not getting the providerchange callback after configuration?

  componentWillMount() {

    BackgroundGeolocation.on('providerchange', (provider) => {
      console.log('background geolocation provider change: ', provider);
    });

    BackgroundGeolocation.configure(
      {
        // Geolocation Config
        desiredAccuracy: 0,
        stationaryRadius: 25,
        distanceFilter: 500,
        // Activity Recognition
        activityRecognitionInterval: 120000,
        stopTimeout: 5,
        heartbeatInterval: 60,
        preventSuspend: true,
        // Application config
        debug: false,
        stopOnTerminate: false,
        startOnBoot: false,
      },
      (state) => {
        console.log('configured: ', state);
      },
    );
  }

In that example I get the 'configured, state... ' log but not the provider change one

christocracy commented 7 years ago

Testing initially with latest from master, I see providerchange fire. providerchange will fire even if plugin isn't yet #started.

SamMatthewsIsACommonName commented 7 years ago

OK so from that same code above, I get the function calling ONLY when I cause a change (i.e allow tracking permission): First I get the configuration log, but nothing to do with provider change.

screen shot 2017-10-27 at 22 19 30

I cause a change, in this instance allowed tracking, and I get the log:

screen shot 2017-10-27 at 22 19 17
christocracy commented 7 years ago

Installing 2.9.2, I'm not seeing providerchange fire.

Try latest installing 2.9.4.

christocracy commented 7 years ago

Have I warned you yet that you should use the private repo react-native-background-geolocation-android, since you're a customer?

SamMatthewsIsACommonName commented 7 years ago

oh yes sorry, I just figured this was a relevant issue. I am using 2.9.5 of that repo. Should we move the discussion to there?

christocracy commented 7 years ago

yes

mwmcode commented 1 year ago

Hi Chris, you might wanna update the documentation to indicate that WhenInUse can be granted on both platforms

currently react-native-background-geolocation/src/declarations/types.d.ts says line 112

whileusing

Android version 11

christocracy commented 1 year ago

@mwmcode Thanks, it will be done in the next release. It is fixed locally now.