flutter_background_geolocation
flutter_background_geolocation copied to clipboard
Api is not being called when app is killed
-
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
- User Checks - In
- start location service
- 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
- 2022-09-19 12:11:39.486 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.AbstractService start]
-
🎾 TrackingService [eventCount: 1] - 2022-09-19 12:11:39.488 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.TrackingService handleLocationResult]
-
╔═════════════════════════════════════════════ -
║ TrackingService: LocationResult -
╠═════════════════════════════════════════════ -
╟─ 📍 Location[fused 18.996729,72.824437 hAcc=13 et=+5d19h42m13s857ms alt=-34.0 vel=0.09940709 bear=214.94812 vAcc=1 sAcc=??? bAcc=??? {Bundle[{}]}] -
╟─ Age: 2468ms, time: 1663569697019 - 2022-09-19 12:11:39.488 13584-18716/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager onLocationResult]
-
╔═════════════════════════════════════════════ -
║ Process LocationResult -
╠═════════════════════════════════════════════ - 2022-09-19 12:11:39.488 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.service.TrackingService performStopDetection]
-
ℹ️ Distance from stoppedAtLocation: -28.395424 - 2022-09-19 12:11:39.488 13584-18716/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 3.4752488
- 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]
- 2022-09-19 12:11:39.520 13584-18711/com.cdpindia.eas I/TSLocationManager: [c.t.l.data.sqlite.b persist]
-
✅ INSERT: e8852e4d-364d-48b8-ab89-487e5c3ebd70 - 2022-09-19 12:11:44.461 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.AbstractService start]
-
🎾 TrackingService [eventCount: 1] - 2022-09-19 12:11:44.466 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.service.TrackingService handleLocationResult]
-
╔═════════════════════════════════════════════ -
║ TrackingService: LocationResult -
╠═════════════════════════════════════════════ -
╟─ 📍 Location[fused 18.996732,72.824433 hAcc=11 et=+5d19h42m18s860ms alt=-34.0 vel=0.09827809 bear=303.14966 vAcc=1 sAcc=??? bAcc=??? {Bundle[{}]}] -
╟─ Age: 2441ms, time: 1663569702022 - 2022-09-19 12:11:44.467 13584-18711/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager onLocationResult]
-
╔═════════════════════════════════════════════ -
║ Process LocationResult -
╠═════════════════════════════════════════════ - 2022-09-19 12:11:44.467 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.service.TrackingService performStopDetection]
-
ℹ️ Distance from stoppedAtLocation: -26.542145 - 2022-09-19 12:11:44.467 13584-18711/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 3.778294
- 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]
- 2022-09-19 12:11:44.530 13584-18716/com.cdpindia.eas I/TSLocationManager: [c.t.l.data.sqlite.b persist]
-
✅ INSERT: 806ae164-2b94-4ff2-ae55-4b03d547b365 - 2022-09-19 12:11:46.840 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.s.TSScheduleManager oneShot]
-
⏰ Scheduled OneShot: TERMINATE_EVENT in 10000ms (jobID: -1708771588) - 2022-09-19 12:11:47.290 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: location - 2022-09-19 12:11:47.291 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: motionchange - 2022-09-19 12:11:47.292 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: activitychange - 2022-09-19 12:11:47.292 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: geofence - 2022-09-19 12:11:47.293 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: heartbeat - 2022-09-19 12:11:47.294 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: http - 2022-09-19 12:11:47.295 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: schedule - 2022-09-19 12:11:47.295 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: connectivitychange - 2022-09-19 12:11:47.296 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: enabledchange - 2022-09-19 12:11:47.297 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: providerchange - 2022-09-19 12:11:47.297 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: powersavechange - 2022-09-19 12:11:47.299 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation removeListener]
-
✅ removeListener event: notificationaction - 2022-09-19 12:11:47.300 13584-13584/com.cdpindia.eas I/TSLocationManager: [c.t.l.s.TSScheduleManager cancelOneShot]
-
⏰ Cancel OneShot: TERMINATE_EVENT - 2022-09-19 12:11:47.302 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.l.LifecycleManager a]
-
╔═════════════════════════════════════════════ -
║ ☯️ HeadlessMode? true -
╠═════════════════════════════════════════════ - 2022-09-19 12:11:47.306 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.f.b.HeadlessTask onHeadlessEvent] 💀 [HeadlessTask terminate]
- 2022-09-19 12:11:47.306 13584-13584/com.cdpindia.eas D/TSLocationManager: [c.t.l.a.BackgroundGeolocation b]
-
🔴 Cleared callbacks - 2022-09-19 12:11:47.308 13584-18711/com.cdpindia.eas I/TSLocationManager: [c.t.l.a.BackgroundGeolocation$l0 run]
-
╔═════════════════════════════════════════════ -
║ MainActivity was destroyed -
╠═════════════════════════════════════════════ -
╟─ stopOnTerminate: false -
╟─ enabled: true - 2022-09-19 12:11:47.308 13584-18716/com.cdpindia.eas E/TSLocationManager: [c.t.f.b.HeadlessTask$TaskRunner run]
-
‼️ Invalid Headless Callback ids. Cannot handle headless event
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.
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??
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 ?
iOS automatically relaunches your app in the background when the device moves about 200 meters from the last known location.
See api docs Config.stopOnTerminate for more information.
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 ?
You must call .ready(config) and register event-listeners each time your app is launched
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
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.