flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

Api is not being called when app is killed

Open dinesh-suthar-cdp opened this issue 3 years ago • 2 comments
trafficstars

  • Plugin version: 3.7.2

  • Platform: Android

  • OS version: 11

  • Device manufacturer / model: OnePlus

  • Flutter info (flutter doctor): [✓] Flutter (Channel stable, 2.10.5, on macOS 12.5.1 21G83 darwin-x64, locale en-IN) [✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.3) [✓] Connected device (2 available) [✓] HTTP Host Availability

  • Plugin config: bg.BackgroundGeolocation.ready( bg.Config( locationUpdateInterval: 10000, desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH, distanceFilter: 0.0, stopOnTerminate: false, startOnBoot: true, debug: true, logLevel: bg.Config.LOG_LEVEL_VERBOSE, reset: true, enableHeadless: true, pausesLocationUpdatesAutomatically: false, ), );

Expected Behavior When the application is killed, OnLocation or onMotionChange API is called

Actual Behavior API is not being called when the application is killed

Steps to Reproduce

  1. User Checks - In
  2. start location service
  3. kill the app

Context

In my app, when the user is check-in for the day the user's location should be tracked w.e.f to time. The app is working fine when the application is in the background and foreground. When the user kills the application, the user's location is not being tracked i.e API is not being called

Debug logs

  1. 2022-09-19 12:11:39.486 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.AbstractService start]
  2.   🎾  TrackingService [eventCount: 1]
    
  3. 2022-09-19 12:11:39.488 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.TrackingService handleLocationResult]
  4. ╔═════════════════════════════════════════════
    
  5. ║ TrackingService: LocationResult
    
  6. ╠═════════════════════════════════════════════
    
  7. ╟─ 📍  Location[fused 18.996729,72.824437 hAcc=13 et=+5d19h42m13s857ms alt=-34.0 vel=0.09940709 bear=214.94812 vAcc=1 sAcc=??? bAcc=??? {Bundle[{}]}]
    
  8. ╟─ Age: 2468ms, time: 1663569697019
    
  9. 2022-09-19 12:11:39.488 13584-18716/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager onLocationResult]
  10. ╔═════════════════════════════════════════════
    
  11. ║ Process LocationResult
    
  12. ╠═════════════════════════════════════════════
    
  13. 2022-09-19 12:11:39.488 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.service.TrackingService performStopDetection]
  14.   ℹ️  Distance from stoppedAtLocation: -28.395424
    
  15. 2022-09-19 12:11:39.488 13584-18716/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 3.4752488
  16. 2022-09-19 12:11:39.488 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish TrackingService [eventCount: 0, sticky: true]
  17. 2022-09-19 12:11:39.520 13584-18711/com.cdpindia.eas I/TSLocationManager: [c.t.l.data.sqlite.b persist]
  18.   ✅  INSERT: e8852e4d-364d-48b8-ab89-487e5c3ebd70
    
  19. 2022-09-19 12:11:44.461 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.AbstractService start]
  20.   🎾  TrackingService [eventCount: 1]
    
  21. 2022-09-19 12:11:44.466 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.TrackingService handleLocationResult]
  22. ╔═════════════════════════════════════════════
    
  23. ║ TrackingService: LocationResult
    
  24. ╠═════════════════════════════════════════════
    
  25. ╟─ 📍  Location[fused 18.996732,72.824433 hAcc=11 et=+5d19h42m18s860ms alt=-34.0 vel=0.09827809 bear=303.14966 vAcc=1 sAcc=??? bAcc=??? {Bundle[{}]}]
    
  26. ╟─ Age: 2441ms, time: 1663569702022
    
  27. 2022-09-19 12:11:44.467 13584-18711/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager onLocationResult]
  28. ╔═════════════════════════════════════════════
    
  29. ║ Process LocationResult
    
  30. ╠═════════════════════════════════════════════
    
  31. 2022-09-19 12:11:44.467 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.service.TrackingService performStopDetection]
  32.   ℹ️  Distance from stoppedAtLocation: -26.542145
    
  33. 2022-09-19 12:11:44.467 13584-18711/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 3.778294
  34. 2022-09-19 12:11:44.468 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish TrackingService [eventCount: 0, sticky: true]
  35. 2022-09-19 12:11:44.530 13584-18716/com.cdpindia.eas I/TSLocationManager: [c.t.l.data.sqlite.b persist]
  36.   ✅  INSERT: 806ae164-2b94-4ff2-ae55-4b03d547b365
    
  37. 2022-09-19 12:11:46.840 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.s.TSScheduleManager oneShot]
  38.   ⏰ Scheduled OneShot: TERMINATE_EVENT in 10000ms (jobID: -1708771588)
    
  39. 2022-09-19 12:11:47.290 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  40.   ✅  removeListener event: location
    
  41. 2022-09-19 12:11:47.291 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  42.   ✅  removeListener event: motionchange
    
  43. 2022-09-19 12:11:47.292 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  44.   ✅  removeListener event: activitychange
    
  45. 2022-09-19 12:11:47.292 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  46.   ✅  removeListener event: geofence
    
  47. 2022-09-19 12:11:47.293 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  48.   ✅  removeListener event: heartbeat
    
  49. 2022-09-19 12:11:47.294 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  50.   ✅  removeListener event: http
    
  51. 2022-09-19 12:11:47.295 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  52.   ✅  removeListener event: schedule
    
  53. 2022-09-19 12:11:47.295 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  54.   ✅  removeListener event: connectivitychange
    
  55. 2022-09-19 12:11:47.296 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  56.   ✅  removeListener event: enabledchange
    
  57. 2022-09-19 12:11:47.297 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  58.   ✅  removeListener event: providerchange
    
  59. 2022-09-19 12:11:47.297 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  60.   ✅  removeListener event: powersavechange
    
  61. 2022-09-19 12:11:47.299 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
  62.   ✅  removeListener event: notificationaction
    
  63. 2022-09-19 12:11:47.300 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.s.TSScheduleManager cancelOneShot]
  64.   ⏰ Cancel OneShot: TERMINATE_EVENT
    
  65. 2022-09-19 12:11:47.302 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.LifecycleManager a]
  66. ╔═════════════════════════════════════════════
    
  67. ║ ☯️  HeadlessMode? true
    
  68. ╠═════════════════════════════════════════════
    
  69. 2022-09-19 12:11:47.306 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.f.b.HeadlessTask onHeadlessEvent] 💀 [HeadlessTask terminate]
  70. 2022-09-19 12:11:47.306 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation b]
  71.   🔴  Cleared callbacks
    
  72. 2022-09-19 12:11:47.308 13584-18711/com.cdpindia.eas I/TSLocationManager: [c.t.l.a.BackgroundGeolocation$l0 run]
  73. ╔═════════════════════════════════════════════
    
  74. ║ MainActivity was destroyed
    
  75. ╠═════════════════════════════════════════════
    
  76. ╟─ stopOnTerminate: false
    
  77. ╟─ enabled: true
    
  78. 2022-09-19 12:11:47.308 13584-18716/com.cdpindia.eas E/TSLocationManager: [c.t.f.b.HeadlessTask$TaskRunner run]
  79.   ‼️  Invalid Headless Callback ids.  Cannot handle headless event
    

dinesh-suthar-cdp avatar Sep 19 '22 06:09 dinesh-suthar-cdp

Your flutter App instance no longer exists in the terminated state.

that’s what “headless mode” is for. See api docs Config.enableHeadless and .registerHeadlessTask. Follow the instructions carefully.

christocracy avatar Sep 19 '22 12:09 christocracy

Your flutter App instance no longer exists in the terminated state.

that’s what “headless mode” is for. See api docs Config.enableHeadless and .registerHeadlessTask. Follow the instructions carefully.

After adding the headless mode I was unable to get shared preference data and also unable to initialise the class. Is there any way to get shared preferences data??

dinesh-suthar-cdp avatar Sep 21 '22 10:09 dinesh-suthar-cdp

Your flutter App instance no longer exists in the terminated state.

that’s what “headless mode” is for. See api docs Config.enableHeadless and .registerHeadlessTask. Follow the instructions carefully.

what about iOS ? If want to call APIs in response of location fetch in kill state ?

Alvish0407 avatar Feb 18 '24 10:02 Alvish0407

iOS automatically relaunches your app in the background when the device moves about 200 meters from the last known location.

christocracy avatar Feb 18 '24 12:02 christocracy

See api docs Config.stopOnTerminate for more information.

christocracy avatar Feb 18 '24 12:02 christocracy

See api docs Config.stopOnTerminate for more information.

In android, I'm calling API to send data from SQL to server manually. In Background & Foreground, using bg.BackgroundGeolocation.onLocation() callback In kill-state, using headlessEvent (Event.LOCATION)

and everything works fine.

But when I re-launch app from notification or from app icon, API calling seems to be stopped. But I still get this in console

D/TSLocationManager(17729): ╟─ 📍 Location[fused 21.154058,72.783717 hAcc=5.0 et=+11h42m55s306ms alt=0.0 vAcc=0.5 vel=2.5002096 sAcc=0.5 bear=340.9997 bAcc=30.0] D/TSLocationManager(17729): ╟─ Age: 14ms, time: 1708262509232

Why the API is not being triggered ?

Alvish0407 avatar Feb 18 '24 13:02 Alvish0407

You must call .ready(config) and register event-listeners each time your app is launched

christocracy avatar Feb 18 '24 13:02 christocracy

You must call .ready(config) and register event-listeners each time your app is launched

Great. It's working.

We don't have to call bg.BackgroundGeolocation.start() because, It's already in that state. Just have to call .ready(config) and register event-listeners

Alvish0407 avatar Feb 18 '24 13:02 Alvish0407

The plug-in is not "ready" to use until .ready(config) resolves. That's why I specifically named that method as such.

Calling .ready does not imply calling .start(). You are not forced to call .start immediately after .ready.

christocracy avatar Feb 18 '24 14:02 christocracy