Almoullim / background_location

Flutter background location plugin for Android and iOS
https://pub.dev/packages/background_location
Other
224 stars 236 forks source link

Background service not running on first call #103

Open donettech opened 3 years ago

donettech commented 3 years ago

I implemented this package to get background location, However, the service won't start on the first call and performs normally on the second and following calls.

`ElevatedButton( child:Text("Start),

onPressed()async{ await bg.BackgroundLocation .setAndroidNotification( title: 'Background service is running', message: 'Background location in progress', icon: '@drawable/ic_notification', ); await bg.BackgroundLocation .setAndroidNotification( title: 'Background service is running', message: 'Background location in progress', icon: '@drawable/ic_notification', ); //await BackgroundLocation.setAndroidConfiguration(1000); await bg.BackgroundLocation .startLocationService( distanceFilter: 200); await bg.BackgroundLocation .startLocationService( distanceFilter: 200); bg.BackgroundLocation.getLocationUpdates( (location) { print("Location updated"+location.toString()); }); } )`

When clicked on the button that calls the service, no error is printed and works fine when clicked for second time.

My flutter doctor is below

Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 2.0.6, on Microsoft Windows [Version 10.0.14393], locale en-IN) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [√] Chrome - develop for the web [√] Android Studio (version 4.1.0) [√] VS Code (version 1.57.1) [√] Connected device (2 available)

luis901101 commented 3 years ago

Same happen to me. This bug began in version ^0.6.1, before that version the notification starts at the first call.

luis901101 commented 3 years ago

Digging a bit more I found that the issue is not only the notification not showing at first call but also the background location not starting. When app goes to background the notification should startForeground as an ongoing notification (to ensure intent service long live), and background location listener should acquire GPS. When all of this is OK then the app notification shows up and the GPS symbol shows up as well proving the app is in deed receiving background locations. In version 0.6.1 this is not working well.

Some examples for better understanding of the issue:

Note 1: the app in the examples uses geolocator ^7.2.0+1 for foreground locations Note 2: In iOS everything is working as expected

blaqkrow commented 3 years ago

Hey man so if you switch back to 0.6.0 it worked? Im having trouble receiving callbacks when I navigate away from the app and into the background. Heres my code: https://github.com/blaqkrow/location_tracking_flutter

luis901101 commented 3 years ago

Hey man so if you switch back to 0.6.0 it worked? Im having trouble receiving callbacks when I navigate away from the app and into the background. Heres my code: https://github.com/blaqkrow/location_tracking_flutter

Yes, using version 0.6.0 works fine, to ensure using that especific version you should set background_location: 0.6.0 in your pubspec, the tip here is not to use the ^ at the begining of the version. I see in your code you don’t set the Android notification, I recommend you to set it, as described in the README of this plugin, await for the notification and then start location service.

DevTiago commented 3 years ago

The answer from @luis901101 saved my day: just change the version from pubspec.yaml from ^0.6.1 to 0.6.0

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.