flutter-geolocator
flutter-geolocator copied to clipboard
how to implement background location(when app terminated)?
💬 Questions and Help
how to implement background location(when app terminated)?
I see a lot of packages for background location but is it impossible to implement this just using geolocator package?
What do you mean by "when device terminated"?
oh I wrote it wrong but I edited, I mean 'when app terminated'
It is possible to get background location even when the app is terminated. Just follow the steps described in: geolocator 9.0.2
The important part to make this possible is to set the foreground notification.
If you start the service (Geolocator.getPositionStream) before closing the app, this foreground notification will keep the service/app alive.
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationText:
"Example app will continue to receive your location even when you aren't using it",
notificationTitle: "Running in Background",
enableWakeLock: true,
)
yes I am using Geolocator.getPositionStream and setted locationSettings like you show me below, also notification icon appear but when I terminated the app, stop to come location data(lat,lon and etc) to my database
Geolocator.getPositionStream(locationSettings: gmsLocationSettings())
.listen((position) {
log('⚙️ \x1B[35m-------> init');
log('${position.latitude}');
log('${position.longitude}');
log('${position.heading}');
final data = LocationData(
lat: position.latitude,
lng: position.longitude,
);
apiCall(data); // here post request
});
You're right, I just tested the coordinate capture when the app is terminated and although the foreground service notification remains active, the coordinate capture stops working.
I believe this has something to do with the location permission request. Because I didn't find the "Always" item that I believe is necessary for the android.permission.ACCESS_BACKGROUND_LOCATION permission.
I requested the permission with the method:
permission = await geolocatorPlatform.requestPermission();
I'm looking into this further, but a response from the developers would be nice.
Always When requesting this permission the user needs to grant permission for the locationWhenInUse permission first, clicking on the Àllow While Using App option on the popup. After allowing the permission the user can request the locationAlways permission and can click on the Change To Always Allow option.
I also request always permission
Future<bool> requestAlwaysPermission() async {
try {
final status = await Geolocator.requestPermission();
return status == LocationPermission.always;
} catch (e) {
log(e.toString());
return false;
}
}
I call below method after calling await geolocatorPlatform.requestPermission();
Thanks,
I just did it! But now, when I kill the app, I got the message on logcat:
2023-02-10 16:31:01.140 7341-7341 FlutterJNI com.test.app W Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 99
So, When I try to open the app, it freezes with the following message:
2023-02-10 16:31:03.578 7341-7341 FlutterGeolocator com.test.app E There is still another flutter engine connected, not stopping location service
@guvanch07
I get the following messages when I kill the app:
D/FlutterGeolocator(28354): Detaching Geolocator from activity E/FlutterGeolocator(28354): Geolocator position updates stopped E/FlutterGeolocator(28354): There is still another flutter engine connected, not stopping location service D/TSBackgroundFetch(28354): ☯️ HeadlessMode? true D/FlutterGeolocator(28354): Flutter engine disconnected. Connected engine count 2 D/FlutterGeolocator(28354): Disposing Geolocator services D/FlutterGeolocator(28354): Tried to stop listening when no MethodChannel had been initialized.
As I said, the foreground service notification continues and apparently the execution too, because of this message:
W/FlutterJNI(32329): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 77
Geolocator is not prepared to receive locations when the app is terminated.
I checked the class GeolocatorPlugin.java and noticed that the location service is being disposed when the FlutterEngine is terminated (which happens when kill the app).
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
unbindForegroundService(binding.getApplicationContext());
dispose();
}
This might work if we can somehow avoid disposing of the location mechanism. Or we somehow share the flutterEngine with the foregroundService. But I still have no idea how to do that.
Maybe this plugin should configure the FlutterEngine directly on the service, if the foregroundNotificationConfig variable is set: Package io.flutter.embedding.engine.plugins.service
Unfortunately I'm thinking of an alternative plugin to replace the geolocator. If you find any, please let me know.
yes, the same massage I got
when I kill the app, connection via usb didn't disconnect with device continue listen but there are not geo data
V/AudioManager(27994): querySoundEffectsEnabled... W/libEGL (27994): EGLNativeWindowType 0x7cc87f9010 disconnect failed W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 554 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 555 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 556 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 557 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 558 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: yandex_mapkit/yandex_map_0. Response ID: 559 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 560 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 561 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 562 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 563 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 564 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 565 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 566 W/FlutterJNI(27994): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter.baseflow.com/geolocator_updates_android. Response ID: 567
@guvanch07
I decided to use this lib background_locator_2, but I needed to make some adjustments because it doesn't work with flutter 3.7.0, so take a look at my fork FORK until they merge my PR.
You can use the library this way:
background_locator_2:
git:
url: https://github.com/tulioccalazans/background_locator_fixed.git
ref: master
I think this library is a good reference for the geolocator team to get some ideas for improvements, because it seems that few things remain to be implemented for the geolocator to work when the app is terminated.
@tulioccalazans okay, thank you)
Hey @tulioccalazans
does your FORK work on iOS when app is terminated ? I have been wandering on internet to find the package for iOS which can fetch the location even when app is terminated. But didn't succeed well.
Currently for android i am using https://github.com/caiobraga/background_locator which is FORK of https://github.com/rekabhq/background_locator which seems to be working fine. Even though, i will try your package too.
I found flutter_background_geolocation, it works fine on simulator but not on real device though. I can say that this library on iOS uses fitness and motion tracking to fetch the location.
Help would be much appreciated.
Hey @tulioccalazans
does your FORK work on iOS when app is terminated ? I have been wandering on internet to find the package for iOS which can fetch the location even when app is terminated. But didn't succeed well.
Currently for android i am using https://github.com/caiobraga/background_locator which is FORK of https://github.com/rekabhq/background_locator which seems to be working fine. Even though, i will try your package too.
I found flutter_background_geolocation, it works fine on simulator but not on real device though. I can say that this library on iOS uses fitness and motion tracking to fetch the location.
Help would be much appreciated.
My FORK works perfectly on Android devices. I'm in the testing phase on iOS devices. But I believe it wouldn't be a problem since the part that needed to be changed was the flutter part and not the native part of the background_locator_2 library. I will carry out the tests in the next few days and any news, I will report here!
I found flutter_background_geolocation, it works fine on simulator but not on real device though
It works perfectly on real devices for the last 9 years (since iOS 7). I test it every day. It never fails.
good evening @tulioccalazans I need your help, in fact when I configure the plugin as you did and I try to take the example from the documentation of background_locator_2, I receive an error with my editor which underlines me this is import below
import 'file_manager.dart'; import 'location_callback_handler.dart'; import 'location_service_repository.dart';
Hi @moudmagongore There is no need to use my FORK because all the PRs were merged. Use the lastest version of background_locator_2.
If it doen't help, please open an issue in background_locator_2 project giving more details about this error.
hello @tulioccalazans i need your help, i am using the latest version of backgroun_locator_2, everything works as expected on android emulator, but when i build the app and i install it in android phone, every time i run the application it closes automatically. Can you help me please ?
hello @tulioccalazans i need your help, i am using the latest version of backgroun_locator_2, everything works as expected on android emulator, but when i build the app and i install it in android phone, every time i run the application it closes automatically. Can you help me please ?
@moudmagongore Sure, please open an issue in background_locator_2 project giving more details about this error.
Use this link to create it: background_locator_2 issues
Attach the isse link in response to this issue so I can find it.
@tulioccalazans here is the link https://github.com/Yukams/background_locator_fixed/issues/84
Is there any progress in this case?
Regards...
up
Bump... Any progress on this case?
up