Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Device Tracking Not Working After App is Terminated #2233

Open
rahul-patil10 opened this issue Dec 18, 2024 · 6 comments
Open

iOS Device Tracking Not Working After App is Terminated #2233

rahul-patil10 opened this issue Dec 18, 2024 · 6 comments

Comments

@rahul-patil10
Copy link

In iOS, the device location service does not start after the app is terminated.

  • Plugin version: "react-native-background-geolocation": "^4.18.2",

  • Platform: IOS

  • OS version: 16.7.10

  • Device manufacturer / model: Iphone 8

  • React Native version (react-native -v): 0.69.4

  • Plugin config : -

    BackgroundGeolocation.ready({
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
    distanceFilter: 0, // meters
    enableHeadless: true,
    stopOnTerminate: false, // Continue tracking in the background
    startOnBoot: true, // Start automatically after device reboot
    debug: true, // Shows debug sounds and notifications (useful during development)
    locationUpdateInterval: 10000, // 30 seconds interval
    fastestLocationUpdateInterval: 10000, // Minimum interval is 15 seconds (for faster passive updates)
    // stationaryRadius: 50,
    useSignificantChangesOnly: false, // Enable SLC
    preventSuspend: true, // Prevent app from suspending when stationary
    heartbeatInterval: 20, // Heartbeat every 30 seconds (or your preferred interval)
    desiredOdometerAccuracy: 50, // Only use locations with accuracy < 30 meters for odometer updates
    logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
    locationAuthorizationRequest: 'Always', // iOS permission
    }, (state) => {
    console.log("BackgroundGeolocation is ready: ", state.enabled);
    });

Expected Behavior

I want to get the location when the app is in the terminated state. Can we use HeadlessTask for iOS?

Actual Behavior

Location is not being retrieved after the app is terminated.

Steps to Reproduce

register in index.js

BackgroundGeolocation.registerHeadlessTask(BackgroundTask);

const BackgroundTask = async (event) => {
console.log('[BackgroundGeolocation HeadlessTask] - Event:', event.name, event.params);
const currentTime = Date.now();
}

@christocracy
Copy link
Member

christocracy commented Dec 18, 2024

There's no such thing as "headless iOS". The API docs explicitly note that headless-mode is an Android-only mechanism.

To learn more about the behaviour of iOS after terminate, see API doc Config.stopOnTerminate

Also see this video

The only way to re-awaken a terminated iOS app is to move at least 200 meters.

@rahul-patil10
Copy link
Author

Thank you for the clarification!

I understand now that:

Headless tasks are Android-only.
For iOS, the app will only re-awaken upon a significant location change (at least 200 meters), as mentioned in the docs and the video you provided.
I'll adjust my implementation accordingly using useSignificantChangesOnly and test with movement beyond 200 meters. If I encounter further issues, I'll follow up here.

Thanks again for the guidance!

@christocracy
Copy link
Member

I'll adjust my implementation accordingly using useSignificantChangesOnly

Why? What do you hope that’s going to do?

@rahul-patil10
Copy link
Author

I aim to track user movements after the app is terminated and log location updates when significant changes (200+ meters) occur. I thought useSignificantChangesOnly would help achieve this. If there's a better approach, please advise.

@rahul-patil10
Copy link
Author

I am not able to trigger my background task after the app has been terminated. The JavaScript code stops executing once the app is terminated, and I cannot seem to execute the task after the app wakes up from termination.

Could you guide me on how to properly configure the library to execute a background task after wake from termination?

I've ensured that the necessary background modes are configured in Info.plist (both location and fetch).
I’ve set stopOnTerminate: false in the configuration for the location services.
I’ve enabled startOnBoot: true to ensure that the service starts after the device reboots.

@christocracy
Copy link
Member

Have you read the API docs for Config.useSignificantChangesOnly? That mode records a location about every 1000 meters. Most of the plugin's Config options are ignored in that mode (eg: distanceFilter stopTimeout, etc).

The easiest way to test iOS is in the Simulator with location simulated with Freeway Drive, as demonstrated in the video I linked above.

Could you guide me on how to properly configure the library to execute a background task after wake from termination?

After app terminate, go outside and move at least 200 meters with the plugin configured with debug: true (or test in the simulator as noted above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants