Baseflow / flutter-geolocator

Android and iOS Geolocation plugin for Flutter
https://baseflow.com/
MIT License
1.24k stars 645 forks source link

Stopping Background Location Service and Unregistering Listeners #1536

Open mayank-vinove opened 2 months ago

mayank-vinove commented 2 months ago

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

Your question

our Android application initiates background location updates upon user login. However, upon user logout, it's essential to gracefully stop the background service responsible for location updates and unregister all associated listeners to optimize resource usage and ensure proper application behavior.

So How can we stop background service and dispose all listeners ( stream) in case of user logout or we want to stop listning the location on simple button click

Version

12.0.0

mayank-vinove commented 2 months ago

any update??

mayank-vinove commented 2 months ago

Is there any update?

I also want cancel the foreground service when the user logs out and start it again when the user re-logs in. Additionally, the foreground service notification should also close when the service is canceled.

Is there any predefined method for this?

mayank-vinove commented 2 months ago

@TimHoogstrate please check this.

mayank-vinove commented 1 month ago

@TimHoogstrate

Summary: Currently, the Geolocator class does not provide a way to access the current active stream subscription. This creates challenges when attempting to manage background location updates, particularly when the app is killed and then reopened.

Proposed Enhancement: It would be highly beneficial to have the ability to retrieve and manage the active stream subscription directly from the Geolocator class. This would allow developers to programmatically stop or cancel the foreground service or stream when the app reopened, The primary goal is to enable better control over background location updates and ensure efficient resource management.

Use Case:

Background Location Updates: When background location updates are enabled and the app is killed, there is no straightforward way to access the active stream subscription upon reopening the app. Providing access to this active stream would allow for proper management of the service. User Logout: On user logout or any specific event, the ability to retrieve and cancel the active stream subscription would ensure that the location updates are stopped as required.

Suggested Implementation:

Introduce a method in the Geolocator class to get the current active stream subscription so it can be handle later from anywhere.

TimHoogstrate commented 3 weeks ago

Dear @mayank-vinove,

Is cancelling the position stream not sufficient like in the example app?

Kind regards,

mayank250004 commented 1 week ago

If the user starts location updates and then kills the app, upon reopening, they lose access to the active stream, making it impossible to cancel.

Possible Solutions: A. Implement a method to return the active stream so that the user can cancel it.

B. Create a method to cancel all active streams and clear the foreground service.

Note : Assume we are not using any third-party plugins like foreground_service or work_manager.