transistorsoft / capacitor-background-geolocation

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
MIT License
96 stars 16 forks source link

Plugin sending same location data to the server when app is closed from recent activity #169

Closed MonaLakshmi2604 closed 5 months ago

MonaLakshmi2604 commented 1 year ago

Your Environment

Expected Behavior

When app is in Background it should push current location data to the server.

Actual Behavior

I can see the new locations in the logs But Its pushing the same data again and again which is got form the foreground.

Steps to Reproduce

Context

Debug logs

``` [c.t.l.l.TSLocationManager onLocationResult] ╔═════════════════════════════════════════════ ║ Process LocationResult ╠═════════════════════════════════════════════ 2023-05-11 15:13:09.325 9614-11664 TSLocationManager io.ionic.starter D [c.t.l.l.TSLocationManager onLocationResult] ℹ️ IGNORED: same as last location 2023-05-11 15:13:09.330 9614-11094 TSLocationManager io.ionic.starter I [c.t.l.data.sqlite.b persist] ✅ INSERT: 30f40ce0-c7c0-4b7a-94c2-b85413d440f1 2023-05-11 15:13:09.338 9614-11094 TSLocationManager io.ionic.starter I [c.t.l.http.HttpService flush] ╔═════════════════════════════════════════════ ║ HTTP Service (count: 1) ╠═════════════════════════════════════════════ 2023-05-11 15:13:09.369 9614-9635 MediaPlayerNative io.ionic.starter V message received msg=10000, ext1=13, ext2=3 2023-05-11 15:13:09.369 9614-9635 MediaPlayerNative io.ionic.starter V message received msg=6, ext1=0, ext2=0 2023-05-11 15:13:09.374 9614-9614 TSLocationManager io.ionic.starter D [c.t.l.service.AbstractService start] 🎾 BackgroundTaskService [eventCount: 1] 2023-05-11 15:13:09.375 9614-9614 TSLocationManager io.ionic.starter I [c.t.l.u.BackgroundTaskManager$Task start] ⏳ startBackgroundTask: 4 2023-05-11 15:13:09.390 9614-11094 TSLocationManager io.ionic.starter D [c.t.l.data.sqlite.b first] ✅ Locked 1 records 2023-05-11 15:13:09.392 9614-11094 TSLocationManager io.ionic.starter I [c.t.l.http.HttpService a] 🔵 HTTP POST: 30f40ce0-c7c0-4b7a-94c2-b85413d440f1 2023-05-11 15:13:09.520 9614-9635 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:09.520 9614-9635 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:09.730 9614-11666 TSLocationManager io.ionic.starter I [c.t.l.http.HttpService$h onResponse] 🔵 Response: 200 2023-05-11 15:13:09.734 9614-11666 TSLocationManager io.ionic.starter D [c.t.l.data.sqlite.b destroy] ✅ DESTROY: 30f40ce0-c7c0-4b7a-94c2-b85413d440f1 2023-05-11 15:13:09.737 9614-11666 TSLocationManager io.ionic.starter I [c.t.l.u.BackgroundTaskManager$Task stop] ⏳ stopBackgroundTask: 4 2023-05-11 15:13:09.740 9614-11666 TSLocationManager io.ionic.starter D [c.t.l.service.AbstractService finish] ⚙️︎ finish BackgroundTaskService [eventCount: 0, sticky: false] 2023-05-11 15:13:09.744 9614-9614 TSLocationManager io.ionic.starter D [c.t.l.service.AbstractService onDestroy] 🔴 BackgroundTaskService stopped 2023-05-11 15:13:10.003 9614-9635 MediaPlayerNative io.ionic.starter V message received msg=2, ext1=0, ext2=0 2023-05-11 15:13:10.003 9614-9635 MediaPlayerNative io.ionic.starter V playback complete 2023-05-11 15:13:10.003 9614-9635 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:10.005 9614-9614 MediaPlayer io.ionic.starter V resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false 2023-05-11 15:13:10.005 9614-9614 MediaPlayer io.ionic.starter V cleanDrmObj: mDrmObj=null mDrmSessionId=null 2023-05-11 15:13:10.005 9614-9614 MediaPlayer-JNI io.ionic.starter V release 2023-05-11 15:13:10.005 9614-9614 MediaPlayerNative io.ionic.starter V setListener 2023-05-11 15:13:10.005 9614-9614 MediaPlayerNative io.ionic.starter V disconnect 2023-05-11 15:13:10.014 9614-9614 MediaPlayerNative io.ionic.starter V destructor 2023-05-11 15:13:10.015 9614-9614 MediaPlayerNative io.ionic.starter V disconnect 2023-05-11 15:13:10.016 9614-9614 MediaPlayer io.ionic.starter W mediaplayer went away with unhandled events 2023-05-11 15:13:17.449 9614-9614 TSLocationManager io.ionic.starter D [c.t.l.service.AbstractService start] 🎾 TrackingService [eventCount: 1] 2023-05-11 15:13:17.457 9614-9614 TSLocationManager io.ionic.starter D [c.t.l.service.TrackingService handleLocationResult] ╔═════════════════════════════════════════════ ║ TrackingService: LocationResult ╠═════════════════════════════════════════════ ╟─ 📍 Location[fused 10.939061,76.978320 hAcc=13.14 et=+1d20h9m27s200ms alt=313.29998779296875 vAcc=1.0104403 vel=0.44850916 bear=323.3069 {Bundle[{}]}] ╟─ Age: 166ms, time: 1683798197286 2023-05-11 15:13:17.459 9614-11094 TSLocationManager io.ionic.starter D [c.t.l.l.TSLocationManager onLocationResult] ╔═════════════════════════════════════════════ ║ Process LocationResult ╠═════════════════════════════════════════════ 2023-05-11 15:13:17.460 9614-11094 TSLocationManager io.ionic.starter D [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 13.728 2023-05-11 15:13:17.465 9614-9614 TSLocationManager io.ionic.starter D [c.t.l.service.AbstractService finish] ⚙️︎ finish TrackingService [eventCount: 0, sticky: true] 2023-05-11 15:13:17.467 9614-11094 MediaPlayer-JNI io.ionic.starter V native_setup 2023-05-11 15:13:17.467 9614-11094 MediaPlayerNative io.ionic.starter V constructor 2023-05-11 15:13:17.468 9614-11094 MediaPlayerNative io.ionic.starter V setListener 2023-05-11 15:13:17.470 9614-11094 MediaPlayer-JNI io.ionic.starter V setParameter: key 1400 2023-05-11 15:13:17.470 9614-11094 MediaPlayerNative io.ionic.starter V MediaPlayer::setParameter(1400) 2023-05-11 15:13:17.470 9614-11094 MediaPlayer-JNI io.ionic.starter V set_session_id(): 18737 2023-05-11 15:13:17.470 9614-11094 MediaPlayerNative io.ionic.starter V MediaPlayer::setAudioSessionId(18737) 2023-05-11 15:13:17.476 9614-11094 MediaPlayer-JNI io.ionic.starter V setDataSourceFD: fd 68 2023-05-11 15:13:17.476 9614-11094 MediaPlayerNative io.ionic.starter V setDataSource(68, 2388328, 44618) 2023-05-11 15:13:17.483 9614-11094 MediaPlayerNative io.ionic.starter V setVideoSurfaceTexture 2023-05-11 15:13:17.484 9614-11094 MediaPlayerNative io.ionic.starter V prepare 2023-05-11 15:13:17.490 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=200, ext1=10973, ext2=0 2023-05-11 15:13:17.490 9614-9742 MediaPlayerNative io.ionic.starter W info/warning (10973, 0) 2023-05-11 15:13:17.491 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=1, ext1=0, ext2=0 2023-05-11 15:13:17.491 9614-9742 MediaPlayerNative io.ionic.starter V MediaPlayer::notify() prepared 2023-05-11 15:13:17.491 9614-9742 MediaPlayerNative io.ionic.starter V signal application thread 2023-05-11 15:13:17.491 9614-11094 MediaPlayerNative io.ionic.starter V prepare complete - status=0 2023-05-11 15:13:17.492 9614-11094 MediaPlayerNative io.ionic.starter V invoke 76 2023-05-11 15:13:17.493 9614-9614 MediaPlayerNative io.ionic.starter V invoke 76 2023-05-11 15:13:17.495 9614-11094 MediaPlayer-JNI io.ionic.starter V start 2023-05-11 15:13:17.495 9614-11094 MediaPlayerNative io.ionic.starter V start 2023-05-11 15:13:17.499 9614-11664 TSLocationManager io.ionic.starter I [c.t.l.data.sqlite.b persist] ✅ INSERT: aa64992b-c2d3-41ae-9e0c-1b1370eb2373 2023-05-11 15:13:17.505 9614-11664 TSLocationManager io.ionic.starter I [c.t.l.http.HttpService flush] ╔═════════════════════════════════════════════ ║ HTTP Service (count: 1) ╠═════════════════════════════════════════════ 2023-05-11 15:13:17.512 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=10000, ext1=13, ext2=3 2023-05-11 15:13:17.515 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=6, ext1=0, ext2=0 2023-05-11 15:13:17.528 9614-9614 TSLocationManager io.ionic.starter D [c.t.l.service.AbstractService start] 🎾 BackgroundTaskService [eventCount: 1] 2023-05-11 15:13:17.529 9614-9614 TSLocationManager io.ionic.starter I [c.t.l.u.BackgroundTaskManager$Task start] ⏳ startBackgroundTask: 5 2023-05-11 15:13:17.537 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:17.539 9614-11664 TSLocationManager io.ionic.starter D [c.t.l.data.sqlite.b first] ✅ Locked 1 records 2023-05-11 15:13:17.540 9614-11664 TSLocationManager io.ionic.starter I [c.t.l.http.HttpService a] 🔵 HTTP POST: aa64992b-c2d3-41ae-9e0c-1b1370eb2373 2023-05-11 15:13:17.660 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:17.660 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:17.661 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:18.138 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=2, ext1=0, ext2=0 2023-05-11 15:13:18.138 9614-9742 MediaPlayerNative io.ionic.starter V playback complete 2023-05-11 15:13:18.139 9614-9742 MediaPlayerNative io.ionic.starter V message received msg=211, ext1=0, ext2=0 2023-05-11 15:13:18.142 9614-9614 MediaPlayer io.ionic.starter V resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false 2023-05-11 15:13:18.142 9614-9614 MediaPlayer io.ionic.starter V cleanDrmObj: mDrmObj=null mDrmSessionId=null 2023-05-11 15:13:18.143 9614-9614 MediaPlayer-JNI io.ionic.starter V release 2023-05-11 15:13:18.143 9614-9614 MediaPlayerNative io.ionic.starter V setListener 2023-05-11 15:13:18.143 9614-9614 MediaPlayerNative io.ionic.starter V disconnect 2023-05-11 15:13:18.152 9614-11666 TSLocationManager io.ionic.starter I [c.t.l.http.HttpService$h onResponse] 🔵 Response: 200 2023-05-11 15:13:18.155 9614-9614 MediaPlayerNative io.ionic.starter V destructor 2023-05-11 15:13:18.156 9614-11666 TSLocationManager io.ionic.starter D [c.t.l.data.sqlite.b destroy] ✅ DESTROY: aa64992b-c2d3-41ae-9e0c-1b1370eb2373 2023-05-11 15:13:18.161 9614-9614 MediaPlayerNative io.ionic.starter V disconnect ```
christocracy commented 1 year ago

They are two different locations. They have different UUID.

✅ INSERT: 30f40ce0-c7c0-4b7a-94c2-b85413d440f1 ✅ INSERT: aa64992b-c2d3-41ae-9e0c-1b1370eb2373

It's not unusual that they happen to have the same latitude / longitude if the device hasn't moved. Particularly if the device is not outdoors (GPS only works outdoors). When indoors, the location comes from Wifi.

christocracy commented 1 year ago

When indoors, the location comes from the latitude / longitude of the Wifi access point, not the actual device location.

MonaLakshmi2604 commented 1 year ago

Actually we are testing in the out door environment only, while app is running in the foreground its works properly but when its running in the background only we are facing this problem and also we have tested more than 5km.

When the app is in foreground call back function is happening, but its not works in background running. Kindly check the debug logs in that call back function is not happening..

christocracy commented 1 year ago

See wiki “Debugging” and learn how to fetch the plug-in log database.

see https://dontkillmyapp.com

MonaLakshmi2604 commented 1 year ago

Hi,

We have followed the methods which you were shared, but still its not working, I have attached the data which was received in our server, till 2023-05-15 13:23:40 only the latitude ,longitude were pushed correctly, the apps runs in the foreground. After that we have closed the app, then its starts to send a repeated latitude and longitude. We have attached the logs in this kindly check that.

background-geolocation.log

image

christocracy commented 1 year ago

Your logs show only one reference of location with uuid: 4cf9d0bf-d8c3-440c-934b-298dc704d188 begin posted to your server.

This is not a problem of the plugin.

05-15 13:23:46.778 DEBUG [TrackingService handleLocationResult] 
╔═════════════════════════════════════════════
║ TrackingService: LocationResult
╠═════════════════════════════════════════════
╟─ 📍  Location[fused 10.938915,76.978285 hAcc=16.914 et=+5d18h19m58s383ms alt=313.6999816894531 vAcc=1.0 vel=0.5937144 bear=359.20853 {Bundle[{}]}]
╟─ Age: 391ms, time: 1684137226382
.
.
.
05-15 13:23:46.844 INFO [b persist] 
  ✅  INSERT: 4cf9d0bf-d8c3-440c-934b-298dc704d188
05-15 13:23:46.849 INFO [HttpService flush] 
╔═════════════════════════════════════════════
║ HTTP Service (count: 1)
╠═════════════════════════════════════════════
.
.
.
05-15 13:23:46.887 DEBUG [b first] 
  ✅  Locked 1 records
05-15 13:23:46.888 INFO [HttpService a] 
  🔵  HTTP POST: 4cf9d0bf-d8c3-440c-934b-298dc704d188
.
.
.
05-15 13:23:47.096 INFO [HttpService$h onResponse] 
  🔵  Response: 200
05-15 13:23:47.098 DEBUG [b destroy] 
  ✅  DESTROY: 4cf9d0bf-d8c3-440c-934b-298dc704d188
MonaLakshmi2604 commented 1 year ago

Hi,

I have attached the log in this, you can see that latitude and longitude were generated but it has not been pushed, Why its pushing the previous latitude and longitude itself, This happens while app is running in the background only. In foreground its works properly, whether its related to any mobile issue?

05-15 13:23:47.851 DEBUG [TrackingService handleLocationResult] ╔═════════════════════════════════════════════ ║ TrackingService: LocationResult ╠═════════════════════════════════════════════ ╟─ 📍 Location[fused 10.938927,76.978282 hAcc=16.11 et=+5d18h19m59s499ms alt=313.6999816894531 vAcc=1.0115529 vel=0.60999686 bear=358.63623 {Bundle[{}]}] ╟─ Age: 349ms, time: 1684137227499

05-15 13:23:47.852 DEBUG [TSLocationManager onLocationResult] ╔═════════════════════════════════════════════ ║ Process LocationResult ╠═════════════════════════════════════════════

05-15 13:23:47.853 DEBUG [TSLocationManager calculateMedianAccuracy] Median accuracy: 17.707 05-15 13:23:47.858 DEBUG [AbstractService finish] ⚙️︎ finish TrackingService [eventCount: 0, sticky: true] 05-15 13:23:47.890 INFO [b persist] ✅ INSERT: 1b676837-c40c-4954-9259-1a078011f583 05-15 13:23:47.894 INFO [HttpService flush] ╔═════════════════════════════════════════════ ║ HTTP Service (count: 1) ╠═════════════════════════════════════════════

05-15 13:23:47.919 DEBUG [AbstractService start] 🎾 BackgroundTaskService [eventCount: 1] 05-15 13:23:47.920 INFO [BackgroundTaskManager$Task start] ⏳ startBackgroundTask: 14 05-15 13:23:47.930 DEBUG [b first] ✅ Locked 1 records 05-15 13:23:47.931 INFO [HttpService a] 🔵 HTTP POST: 1b676837-c40c-4954-9259-1a078011f583 05-15 13:23:48.099 INFO [HttpService$h onResponse] 🔵 Response: 200 05-15 13:23:48.101 DEBUG [b destroy] ✅ DESTROY: 1b676837-c40c-4954-9259-1a078011f583

christocracy commented 1 year ago

but it has not been pushed

It was "pushed" right here:

05-15 13:23:47.931 INFO [HttpService a]
🔵 HTTP POST: 1b676837-c40c-4954-9259-1a078011f583
MonaLakshmi2604 commented 1 year ago

Hi,

Yes it has pushed but actually not getting into our server, but while app runs in foreground the pushing data is captured in our server. When we kills the app in foreground the last service which we got in foreground is repeatedly pushing. Is it possible to connect in teams or google meet for verify our code is correct or not.

christocracy commented 1 year ago

When we kills the app in foreground the last service which we got in foreground is repeatedly pushing.

You've configured the plugin to stopOnTerminate: false with a distanceFilter: 1? Of course the plugin continues to record locations after terminate.

possible to connect in teams or google meet for verify our code is correct or not.

I do 1 hour screen-sharing sessions for $500: https://shop.transistorsoft.com/products/video-conference?utm_source=copyToPasteBoard&utm_medium=product-links&utm_content=web

MonaLakshmi2604 commented 1 year ago

is my config correct?

ngAfterContentInit() { // /// Step 1: Subscribe to BackgroundGeolocation events. this.subscriptions.push(BackgroundGeolocation.onLocation((location) => { this.addEvent('onLocation', location); })) this.subscriptions.push(BackgroundGeolocation.onMotionChange((event) => { this.addEvent('onMotionChange', event); })) this.subscriptions.push(BackgroundGeolocation.onActivityChange((event) => { this.addEvent('onActivityChange', event); })) this.subscriptions.push(BackgroundGeolocation.onProviderChange((event) => { this.addEvent('onProviderChange', event); })) /// Step 2: Ready the plugin. BackgroundGeolocation.ready({ desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH, distanceFilter: 1, stopTimeout: 5, debug: true, // <-- enable this hear sounds for background-geolocation life-cycle. logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE, enableHeadless: true, stopOnTerminate: false,
// <-- Allow the background-service to continue tracking when user closes the app. startOnBoot: true, // <-- Auto start tracking when device is powered-up. }).then((state) => { // BackgroundGeolocation is now ready to use. this. Ready = true; this. Enabled = state.enabled; this.addEvent('State', state); });

}

addEvent(name:string, event:any) {

BackgroundGeolocation.setConfig({
  //extras: {route_id: 1234},
  url: 'http://mydomain.com/insert_bg_data.php?api_key=this.api&uuid='+event.uuid+'&is_moving='+event.is_moving+'&timestamp='+event.timestamp+'&latitude='+event.coords.latitude+'&longitude='+event.coords.longitude+'&speed='+event.coords.speed+'&is_charging='+event.battery.is_charging+'&battery_percentage='+event.battery.level,

})
this.zone.run(() => {

  this.events.push({
    name: name, 
    json: JSON.stringify(event, null, 2)
  })  
})    

}

christocracy commented 1 year ago

Why do you have all this useless this.addEvent('onLocation', location); stuff copy/pasted from the /example app? Do you even know what it does? It’s useless demo code to add events to a List in the /example app.

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.