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

iOS Background Task Being Forcekilled

Open kimihiro64 opened this issue 5 years ago • 9 comments

Your Environment

  • Plugin version: 3.9.1
  • Platform: iOS
  • OS version: 14+
  • Device manufacturer / model: iPhone 12
  • React Native version (react-native -v): 0.63.3
  • Plugin config
{
    activityRecognitionInterval = 10000;
    activityType = 1;
    authorization =    {
    };
    autoSync = 1;
    autoSyncThreshold = 0;
    batchSync = 0;
    debug = 0;
    desiredAccuracy = "-1";
    desiredOdometerAccuracy = 100;
    didLaunchInBackground = 0;
    didRequestUpgradeLocationAuthorization = 0;
    disableAutoSyncOnCellular = 0;
    disableElasticity = 0;
    disableLocationAuthorizationAlert = 0;
    disableMotionActivityUpdates = 1;
    disableStopDetection = 1;
    distanceFilter = 10;
    elasticityMultiplier = 1;
    enableTimestampMeta = 0;
    enabled = 0;
    encrypt = 0;
    extras =    {
        "app_name" = <hidden>;
        "app_version" = "6.0.599";
        "badge_count" = 0;
        "device_id" = "<hidden>";
        "device_model" = iPhone;
        "device_os" = iOS;
        "device_os_version" = "14.3";
        "device_token" = <hidden>;
        id = 107025;
        "location_service_preference" = always;
        "location_services" = 1;
        "mobile_phone" = "";
        "push_notifications" = 1;
        user = "<null>";
    };
    geofenceInitialTriggerEntry = 1;
    geofenceProximityRadius = 2000;
    geofenceTemplate = "";
    headers =    {
        Authorization = "<hidden>";
    };
    heartbeatInterval = 60;
    httpRootProperty = ".";
    httpTimeout = 60000;
    iOSHasWarnedLocationServicesOff = 0;
    isFirstBoot = 0;
    isMoving = 0;
    lastLocationAuthorizationStatus = 3;
    locationAuthorizationAlert =    {
        cancelButton = Cancel;
        instructions = "To use background location, you must enable '{locationAuthorizationRequest}' in the Location Services settings";
        settingsButton = Settings;
        titleWhenNotEnabled = "Background location is not enabled";
        titleWhenOff = "Location services are off";
    };
    locationAuthorizationRequest = Always;
    locationTemplate = "{\"last_reported_location_lat\":<%= latitude %>,\"last_reported_location_lng\":<%= longitude %>,\"last_reported_location_date\":\"<%= timestamp %>\",\"battery_level\":<%= battery.level %>}";
    locationTimeout = 60;
    locationsOrderDirection = ASC;
    logLevel = 5;
    logMaxDays = 3;
    maxBatchSize = "-1";
    maxDaysToPersist = 1;
    maxRecordsToPersist = "-1";
    method = POST;
    minimumActivityRecognitionConfidence = 70;
    odometer = 0;
    params =    {
        "auth_token" = <hidden>;
    };
    pausesLocationUpdatesAutomatically = 1;
    persistMode = 2;
    preventSuspend = 1;
    schedule =    (
    );
    schedulerEnabled = 0;
    showsBackgroundLocationIndicator = 1;
    startOnBoot = 1;
    stationaryRadius = 25;
    stopAfterElapsedMinutes = "-1";
    stopDetectionDelay = 0;
    stopOnStationary = 0;
    stopOnTerminate = 0;
    stopTimeout = 5;
    trackingMode = 1;
    url = "<hidden>";
    useSignificantChangesOnly = 0;
}


Expected Behavior

App should remain running in the background (for at least 8 hours) without being killed.

Actual Behavior

App runs for an arbitrary time before the background task eventually being killed (could be minutes or hours).

Steps to Reproduce

  1. Start a background location task with heartbeat
  2. Keep app backgrounded
  3. Eventually the background task and thus heartbeats will stop

Context

We need to make a request to server every 20 seconds while user has an active session, even if in the background.

Debug logs

Logs
(relevant parts of log)
2020-12-16 18:36:32.703 :large_blue_circle:-[BackgroundTaskManager onPreventSuspendTimer:] Prevent-suspend timer fired! (bg time remaining: 15.109556)
2020-12-16 18:37:32.636 :large_blue_circle:-[BackgroundTaskManager onPreventSuspendTimer:] Prevent-suspend timer fired! (bg time remaining: 10.476298)
2020-12-16 18:40:13.435 :warning:-[BackgroundTaskManager createBackgroundTask]_block_invoke FORCE KILL BACKGROUND TASK: 56, remaining time: 4.036710
2020-12-16 18:40:13.436 :white_check_mark:-[BackgroundTaskManager stopBackgroundTask:]_block_invoke 56 OF (56)
2020-12-16 18:46:02.814 :large_blue_circle:-[TSLocationManager onHeartbeat] Heartbeat

kimihiro64 avatar Dec 17 '20 02:12 kimihiro64

preventSuspend is a hack and iOS seems to have finally put the hammer down on it. Do not rely upon preventSuspend for iOS.

christocracy avatar Dec 17 '20 03:12 christocracy

When an iOS device moves by at least 200 meters, the plugin will cause a terminated or suspended app to come alive and tracking will resume.

christocracy avatar Dec 17 '20 03:12 christocracy

Right... But if we lose contact with the device (this is a safety monitoring feature) for an extended period, we have to call the user and potentially alert law enforcement. So we're trying to figure out how to just keep the app going (like a navigation app).

kimihiro64 avatar Dec 17 '20 03:12 kimihiro64

As long as iOS location-services are on (ie the plugin is in the moving state, and tracking the device), your app is completely alive in the background.

christocracy avatar Dec 17 '20 09:12 christocracy

Is there a way to keep the location-services on continually, even if device is not moving?

kimihiro64 avatar Dec 17 '20 16:12 kimihiro64

Is there a way to keep the location-services on continually, even if device is not moving?

You will kill the battery in a couple of hours if not plugged into power.

See API docs

  • BackgroundGeolocation.changePace
  • Config.pausesLocationUpdatesAutomatically
  • Config.disableStopDetection

christocracy avatar Dec 17 '20 16:12 christocracy

preventSuspend is a hack and iOS seems to have finally put the hammer down on it. Do not rely upon preventSuspend for iOS.

@christocracy can you point to or provide any more information about this? I just happened to find this comment while looking for something else, and I'm now wondering if preventSuspend should be completely removed from our config, and if so what that means for heartbeatInterval on iOS, which relies on it.

omnikron avatar Jan 12 '21 15:01 omnikron

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 Mar 20 '21 00:03 stale[bot]

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

stale[bot] avatar Jun 02 '21 17:06 stale[bot]