react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

Start geolocation with the app in background (Android)

Open martinfrouin opened this issue 4 years ago • 13 comments

Your Environment

  • Plugin version: 4.0.2
  • Platform: Android
  • OS version: 10
  • Device manufacturer / model: OnePlus Nord 100T
  • React Native version (react-native -v): 0.62.2
  • Plugin config
      debug: true,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: 10,
      stopOnTerminate: true,
      reset: true,
      foregroundService: true,
      preventSuspend: true,
      showsBackgroundLocationIndicator: true,

Expected Behavior

With the app in background, The BackgroundGeolocation.start Promise should be rejected if it doesn't really start.

Actual Behavior

The BackgroundGeolocation.start Promise is resolved, and we got enabled: true

Steps to Reproduce

Just call BackgroundGeolocation.start while your app is in background (with a setTimeout for example)

setTimeout(() => {
    BackgroundGeolocation.start((state) => {
      console.log(state.enabled)
    }).catch((err) => {
      console.log('error', err)
    })
  }, 5000)

Context

There's a loader with a lot of action before starting the geolocation on the app i'm developing. If the user is putting his app in background at this moment, I can't handle the error properly.

I also tried on the demo app, and the issue is here as well

martinfrouin avatar Jul 08 '21 14:07 martinfrouin

Why do you think the plugin should not start if executed in the background?

I don't see any issue here.

christocracy avatar Jul 08 '21 14:07 christocracy

If i'm not doing something wrong, for me there are 2 issues:

  • Why the plugin is not starting if the start function is executed in the background ?
  • Why the start Promise is resolved, if there is an error ?

I can provide you a video if needed.

martinfrouin avatar Jul 08 '21 15:07 martinfrouin

Video is not interesting. Are you watching the plugin logs?

What makes you think the plugin is not starting? Have you read the wiki Philosophy of Operation? This plugin tracks location only when the device is detected to be physically moving.

christocracy avatar Jul 08 '21 15:07 christocracy

Yep, i've read it. I think the plugin is not starting because I don't have any onLocation logs if i use this func in background. I'm using Lockito for my tests, and it's working perfectly if i'm starting it on foreground

martinfrouin avatar Jul 08 '21 15:07 martinfrouin

The plugin does not trigger into the moving state from mock location apps, such as Lockito,

It triggers into the moving state from Motion API (accelerometer, gyroscope, magnetometer).

This is noted in the wiki Debugging.

christocracy avatar Jul 08 '21 15:07 christocracy

Yes I know, i'm moving my phone at the same time, and it's working fine if i'm starting the plugin in foreground.

martinfrouin avatar Jul 08 '21 15:07 martinfrouin

Are you monitoring adb logcat, as suggested in wiki Debugging? This is the only way to know what's going on.

christocracy avatar Jul 08 '21 15:07 christocracy

And I never trust JavaScript setTimeout in the background.

christocracy avatar Jul 08 '21 15:07 christocracy

Also, executing BackgroundGeolocation.start() is not something one would normally execute in the background. One would normally execute this when your app boots.

christocracy avatar Jul 08 '21 18:07 christocracy

On the demo app, the configure is called on the componentDidMount, and the start is on the toggle button. I just did the same on my app.

I have on logCat

07-09 11:04:24.141  2008  2135 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.transistorsoft.backgroundgeolocation.react flg=0x4000010 (has extras) } to com.android.settings/com.oneplus.settings.receiver.SettingsReceiver

So I can't use start with my app in background. If it's not possible, it's not a problem for me. The real issue here is that the start function is resolving the promise with enabled: true on the callback instead of rejecting the promise. So I can't handle it properly

martinfrouin avatar Jul 09 '21 09:07 martinfrouin

Hi @christocracy, we are still on this, could u check and answer the previous message from tinmar33 please ?

BenoitFroment avatar Sep 06 '21 09:09 BenoitFroment

@christocracy just a note: setTimeout in Android headless tasks should start working with react-native 0.69.x - PR that should fix it was just merged and 0.69-release branch hasn't been cut yet - https://github.com/facebook/react-native/pull/33044

mikehardy avatar Apr 11 '22 14:04 mikehardy

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] avatar Jun 12 '22 19:06 stale[bot]