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.64k stars 425 forks source link

Question about preventSuspend #2056

Closed felipemelendez closed 4 months ago

felipemelendez commented 4 months ago

Your Environment

For BackgroundGeolocation

BackgroundGeolocation.ready(
    {
        desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_NAVIGATION,
        distanceFilter: 5,
        stationaryRadius: 25,
        startOnBoot: true,
        enableHeadless: true,
        stopOnTerminate: false,
        preventSuspend: true,
        heartbeatInterval: 60,
        locationUpdateInterval: 1000,
        fastestLocationUpdateInterval: 500,
        disableLocationAuthorizationAlert: true,
        logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
        debug: true,
    },
    (state) => {
        if (!state.enabled) {
            BackgroundGeolocation.start();
        }
    }
);

For BackgroundFetch

BackgroundFetch.configure(
{
    minimumFetchInterval: 15,
    stopOnTerminate: false,
    startOnBoot: true,
    enableHeadless: true,
    requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE,
    requiresCharging: false,
    requiresDeviceIdle: false,
    requiresBatteryNotLow: false,
    requiresStorageNotLow: false,
},
onEvent,
onTimeout
);

Expected Behavior

If the app has been terminated, I want to still be able to update the db with the user's location when the user exits a stationaryRadius of 25 meters.

Context

I am developing an app where the predominant activity will be walking. The app will not have a button to manually switch the activity (not an exercise app), but there will be a range of dates in which I want to be able to start the background location when the user moves outside a stationaryRadius of 25 meters.

For this, I am leveraging react-native-background-geolocation and react-native-background-fetch

From the docs I see that stopOnTerminate enables a backup motion-detection mechanism with a geofence radius of 200 meters - that is too large for pedestrian activity.

I see that with iOS, 200 meters is a minimum requirement, but then there's preventSuspend, which I believe can help reduce or eliminate that minimum radius if Apple approves background location capability.

Questions

onLocation is not working with that configuration, do I need to use onHearbeat to leverage preventSuspend?

If I do need to use onHeartbeat, and the system wakes up when the user exits the stationaryRadius will it remain awake even after the user stops moving?

OR, am I misunderstanding this completely and the only way for the system to wake up and perform a location update is to wait 15 minutes (i.e. minimumFetchInterval: 15)?

christocracy commented 4 months ago

You don’t need any of that.

preventSuspend doesn’t work after terminate. 99% of use-cases don’t need preventSuspend. You probably don’t need it.

the minimum distance to travel for tracking to engage (while in background / terminated) is 200 meters.

This is all easily experimented with in the iOS Simulator with location simulated with Freeway Drive and debug: true enabled.

felipemelendez commented 4 months ago

This is all easily experimented with in the iOS Simulator with location simulated with Freeway Drive and debug: true enabled.

Ever since I installed this API, I have not been able to use the iOS Simulator, I get this error when I try to run it: ERROR Invariant Violation: Your JavaScript code tried to access a native module that doesn't exist.

So I've been testing on a development build.

christocracy commented 4 months ago

I suggest you Google that and figure out how to fix it.

You’re wasting your time not being able to quickly experiment, answering questions in 30s that take 20 min of walking outside to test.

felipemelendez commented 4 months ago

yep, I had been testing by taking my dog for a walk around the block, after this exchange I decided to hop in the car and it turns out that my block has a radius < 200 meters. As soon as I exited the 200 meter radius the debugger came to life.

I wish we could make that radius smaller somehow...

felipemelendez commented 4 months ago

Adding this for posterity.

I am using Expo and my problem with the simulator was solved by creating a development build and installing the build in the simulator.

Here are the respective docs: Creating a dev build: https://docs.expo.dev/develop/development-builds/create-a-build/

Installing dev build in the simulator: https://docs.expo.dev/build-reference/simulators/#installing-build-on-the-simulator