Skip to content

[Bug]: ForegroundServiceDidNotStartInTimeException #1633

@tananaev

Description

@tananaev

Required Reading

  • Confirmed

Plugin Version

5.0.3

Flutter Doctor

[✓] Flutter (Channel stable, 3.38.9, on macOS 26.2 25C56 darwin-arm64, locale
    en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 26.0.1)
[✓] Chrome - develop for the web
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

Mobile operating-system(s)

  • iOS
  • Android

Device Manufacturer(s) and Model(s)

Wide variety, including Pixels

Device operating-systems(s)

13 - 16

What happened?

We see a very large percentage of foreground service couldn't start on time. I think percentage is way above reasonable. I see 1% user perceived rate in Google Play and about 2% of crash free sessions in Crashlytics.

Almost all errors are happening in headless service mode. Our code looks like this:

@pragma('vm:entry-point')
void headlessTask(bg.HeadlessEvent headlessEvent) async {
  await Firebase.initializeApp();
  await Preferences.init();
  FirebaseCrashlytics.instance.log('geolocation_headless:${headlessEvent.name}');
  switch (headlessEvent.name) {
    case bg.Event.ENABLEDCHANGE:
      await GeolocationService.onEnabledChange(headlessEvent.event);
    case bg.Event.MOTIONCHANGE:
      await GeolocationService.onMotionChange(headlessEvent.event);
    case bg.Event.HEARTBEAT:
      await GeolocationService.onHeartbeat(headlessEvent.event);
    case bg.Event.LOCATION:
      await GeolocationService.onLocation(headlessEvent.event);
  }
}

And I often see long logs before the crash:

{
  "title": "Crashlytics - Custom logs",
  "bundle_identifier": "org.traccar.client",
  "platform": "android",
  "display_version": "9.7.6",
  "build_version": "120",
  "issue_id": "bf0aa132fee08d894d95a404d1a44d14",
  "session_id": "698B4E0003CF000114E5CA741E0C0A94_DNE_0_v2",
  "event_timestamp": "Tue Feb 10 2026 07:26:22 GMT-0800 (Pacific Standard Time)",
  "logs_and_breadcrumbs": [
    {
      "timestamp": "Tue Feb 10 2026 07:25:55 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:25:55 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:http",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:25:55 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:http",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:11 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:11 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:15 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:15 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:http",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:16 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:activitychange",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:16 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:16 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_motion:true",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:16 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:motionchange",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:19 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:20 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:21 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:activitychange",
      "source": "crashlytics"
    },
    {
      "timestamp": "Tue Feb 10 2026 07:26:21 GMT-0800 (Pacific Standard Time)",
      "message": "geolocation_headless:location",
      "source": "crashlytics"
    }
  ]
}

This seems incorrect because the first thing foreground service should do is register as a foreground service, but based on the logs it seems like it's done a lot of other work and not registered, which I think excludes the possibility that it's just a slow device.

Plugin Code and/or Config

{
  "actions": [],
  "activity": {
    "activityRecognitionInterval": 10000,
    "disableMotionActivityUpdates": false,
    "disableStopDetection": false,
    "minimumActivityRecognitionConfidence": 75,
    "motionTriggerDelay": 0,
    "stopOnStationary": false,
    "triggerActivities": "in_vehicle, on_bicycle, on_foot, running, walking"
  },
  "activityRecognitionInterval": 10000,
  "allowIdenticalLocations": false,
  "allowTap": true,
  "app": {
    "backgroundPermissionRationale": {
      "message": "For reliable tracking, please enable {backgroundPermissionOptionLabel} location access.",
      "negativeAction": "Cancel",
      "positiveAction": "Change to {backgroundPermissionOptionLabel}",
      "title": "Allow {applicationName} to access this device's location in the background"
    },
    "enableHeadless": true,
    "foregroundService": true,
    "headlessJobService": "com.transistorsoft.flutter.backgroundgeolocation.HeadlessTask",
    "heartbeatInterval": -1,
    "mainActivityName": null,
    "notification": {
      "actions": [],
      "allowTap": true,
      "channelDescription": "Location tracking",
      "channelId": "bggeo",
      "channelName": "BackgroundGeolocation",
      "color": "",
      "importance": 2,
      "largeIcon": "",
      "layout": "",
      "priority": -1,
      "smallIcon": "drawable\/ic_stat_notify",
      "sticky": false,
      "strings": {},
      "tapActivity": "",
      "text": "Tracking location",
      "title": "Background Geolocation"
    },
    "schedule": [],
    "scheduleUseAlarmManager": false,
    "startOnBoot": true,
    "stopOnTerminate": false
  },
  "authorization": {
    "accessToken": "✱✱✱",
    "expires": -1,
    "refreshHeaders": {},
    "refreshPayload": {},
    "refreshToken": "✱✱✱",
    "refreshUrl": "✱✱✱",
    "strategy": "JWT"
  },
  "autoSync": false,
  "autoSyncThreshold": 0,
  "backgroundPermissionRationale": {
    "message": "For reliable tracking, please enable {backgroundPermissionOptionLabel} location access.",
    "negativeAction": "Cancel",
    "positiveAction": "Change to {backgroundPermissionOptionLabel}",
    "title": "Allow {applicationName} to access this device's location in the background"
  },
  "batchSync": false,
  "channelDescription": "Location tracking",
  "channelId": "bggeo",
  "channelName": "BackgroundGeolocation",
  "color": "",
  "debug": false,
  "deferTime": 0,
  "desiredAccuracy": 10,
  "didDeviceReboot": true,
  "didShowBackgroundPermissionRationale": false,
  "disableAutoSyncOnCellular": false,
  "disableElasticity": true,
  "disableLocationAuthorizationAlert": false,
  "disableMotionActivityUpdates": false,
  "disableProviderChangeRecord": false,
  "disableStopDetection": false,
  "distanceFilter": 75,
  "elasticityMultiplier": 1,
  "enableHeadless": true,
  "enableTimestampMeta": false,
  "enabled": false,
  "extras": {},
  "fastestLocationUpdateInterval": 30000,
  "foregroundService": true,
  "geofenceInitialTriggerEntry": true,
  "geofenceModeHighAccuracy": true,
  "geofenceProximityRadius": 1000,
  "geofenceTemplate": "",
  "geolocation": {
    "allowIdenticalLocations": false,
    "deferTime": 0,
    "desiredAccuracy": 10,
    "disableElasticity": true,
    "disableLocationAuthorizationAlert": false,
    "distanceFilter": 75,
    "elasticityMultiplier": 1,
    "enableTimestampMeta": false,
    "fastestLocationUpdateInterval": 30000,
    "filter": {
      "burstWindow": 10,
      "filterDebug": false,
      "kalmanDebug": false,
      "kalmanProfile": 0,
      "maxBurstDistance": 300,
      "maxImpliedSpeed": 60,
      "odometerAccuracyThreshold": 20,
      "odometerUseKalmanFilter": true,
      "policy": 2,
      "rollingWindow": 5,
      "trackingAccuracyThreshold": 100,
      "useKalman": true
    },
    "geofenceInitialTriggerEntry": true,
    "geofenceModeHighAccuracy": true,
    "geofenceProximityRadius": 1000,
    "locationAuthorizationRequest": "Always",
    "locationTimeout": 60,
    "locationUpdateInterval": 300000,
    "stationaryRadius": 150,
    "stopAfterElapsedMinutes": 0,
    "stopTimeout": 5,
    "useCLLocationAccuracy": false,
    "useSignificantChangesOnly": false
  },
  "headers": {},
  "headlessJobService": "com.transistorsoft.flutter.backgroundgeolocation.HeadlessTask",
  "heartbeatEnabled": false,
  "heartbeatInterval": -1,
  "http": {
    "autoSync": false,
    "autoSyncThreshold": 0,
    "batchSync": false,
    "disableAutoSyncOnCellular": false,
    "headers": {},
    "maxBatchSize": -1,
    "method": "POST",
    "params": {
      "device_id": "53040646"
    },
    "rootProperty": "location",
    "timeout": 60000,
    "url": "http:\/\/demo.traccar.org:5055\/"
  },
  "httpRootProperty": "location",
  "httpTimeout": 60000,
  "importance": 2,
  "isFirstBoot": false,
  "isMoving": false,
  "largeIcon": "",
  "layout": "",
  "locationAuthorizationRequest": "Always",
  "locationTemplate": "{\"timestamp\": \"<%= timestamp %>\",\"coords\": {\"latitude\": <%= latitude %>,\"longitude\": <%= longitude %>,\"accuracy\": <%= accuracy %>,\"speed\": <%= speed %>,\"heading\": <%= heading %>,\"altitude\": <%= altitude %>},\"is_moving\": <%= is_moving %>,\"odometer\": <%= odometer %>,\"event\": \"<%= event %>\",\"battery\": {\"level\": <%= battery.level %>,\"is_charging\": <%= battery.is_charging %>},\"activity\": {\"type\": \"<%= activity.type %>\"},\"extras\": {},\"_\": \"&id=53040646&lat=<%= latitude %>&lon=<%= longitude %>&timestamp=<%= timestamp %>&\"}",
  "locationTimeout": 60,
  "locationUpdateInterval": 300000,
  "locationsOrderDirection": "ASC",
  "logLevel": 5,
  "logMaxDays": 1,
  "logger": {
    "debug": false,
    "logLevel": 5,
    "logMaxDays": 1
  },
  "maxBatchSize": -1,
  "maxDaysToPersist": 1,
  "maxRecordsToPersist": -1,
  "method": "POST",
  "minimumActivityRecognitionConfidence": 75,
  "motionTriggerDelay": 0,
  "notification": {
    "actions": [],
    "allowTap": true,
    "channelDescription": "Location tracking",
    "channelId": "bggeo",
    "channelName": "BackgroundGeolocation",
    "color": "",
    "importance": 2,
    "largeIcon": "",
    "layout": "",
    "priority": -1,
    "smallIcon": "drawable\/ic_stat_notify",
    "sticky": false,
    "strings": {},
    "tapActivity": "",
    "text": "Tracking location",
    "title": "Background Geolocation"
  },
  "odometer": 0,
  "odometerError": 0,
  "params": {
    "device_id": "53040646"
  },
  "persistMode": 2,
  "persistence": {
    "disableProviderChangeRecord": false,
    "extras": {},
    "geofenceTemplate": "",
    "locationTemplate": "{\"timestamp\": \"<%= timestamp %>\",\"coords\": {\"latitude\": <%= latitude %>,\"longitude\": <%= longitude %>,\"accuracy\": <%= accuracy %>,\"speed\": <%= speed %>,\"heading\": <%= heading %>,\"altitude\": <%= altitude %>},\"is_moving\": <%= is_moving %>,\"odometer\": <%= odometer %>,\"event\": \"<%= event %>\",\"battery\": {\"level\": <%= battery.level %>,\"is_charging\": <%= battery.is_charging %>},\"activity\": {\"type\": \"<%= activity.type %>\"},\"extras\": {},\"_\": \"&id=53040646&lat=<%= latitude %>&lon=<%= longitude %>&timestamp=<%= timestamp %>&\"}",
    "locationsOrderDirection": "ASC",
    "maxDaysToPersist": 1,
    "maxRecordsToPersist": -1,
    "persistMode": 2
  },
  "priority": -1,
  "schedule": [],
  "scheduleUseAlarmManager": false,
  "schedulerEnabled": false,
  "smallIcon": "drawable\/ic_stat_notify",
  "startOnBoot": true,
  "stationaryRadius": 150,
  "sticky": false,
  "stopAfterElapsedMinutes": 0,
  "stopOnStationary": false,
  "stopOnTerminate": false,
  "stopTimeout": 5,
  "strings": {},
  "tapActivity": "",
  "text": "Tracking location",
  "title": "Background Geolocation",
  "trackingMode": 1,
  "triggerActivities": "in_vehicle, on_bicycle, on_foot, running, walking",
  "url": "http:\/\/demo.traccar.org:5055\/",
  "useCLLocationAccuracy": true,
  "useSignificantChangesOnly": false
}

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions