flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

[Bug]: The app is still running in background event if it's killed

Open robpot95 opened this issue 1 year ago • 22 comments

Please check the following before submitting a new issue.

Please select affected platform(s)

  • [X] Android
  • [ ] iOS
  • [ ] Linux
  • [ ] macOS
  • [ ] Web
  • [ ] Windows

Steps to reproduce

Just have notification configured and run the app. When you kill it you get: There is still another flutter engine connected, not stopping location service

Expected results

The app should be killed and notification should be removed

Actual results

The app still is running and notification is still there. This is draining users battery.

Code sample

Code sample
[Paste your code here]

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

10.1.0

Flutter Doctor output

Doctor output
[Paste your output here]

robpot95 avatar Dec 18 '23 10:12 robpot95

Dear @robpot95,

Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code?

Kind regards,

TimHoogstrate avatar Jan 04 '24 10:01 TimHoogstrate

Dear @robpot95,

Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code?

Kind regards,

Hello,

I have not tested on the example project. But i've searched bit aroud issues in the github. Some say it may be that it attach to the FCM background service when you try to kill the app.

Also this happens only if i configure foregroundNotificationConfig

robpot95 avatar Jan 04 '24 10:01 robpot95

Same thing here, the foregroundNotificationConfig ensures that the notification is non dismissible even if the application has been terminated, it has not been the case for me that the app remains alive. But the notification seems permanent.

Dear @robpot95, Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code? Kind regards,

Hello,

I have not tested on the example project. But i've searched bit aroud issues in the github. Some say it may be that it attach to the FCM background service when you try to kill the app.

Also this happens only if i configure foregroundNotificationConfig

XERZES27 avatar Jan 06 '24 20:01 XERZES27

Same thing here, the foregroundNotificationConfig ensures that the notification is non dismissible even if the application has been terminated, it has not been the case for me that the app remains alive. But the notification seems permanent.

Dear @robpot95, Is this also happening in the example app? Or do you have some minimal steps to reproduce or perhaps a little example app/code? Kind regards,

Hello, I have not tested on the example project. But i've searched bit aroud issues in the github. Some say it may be that it attach to the FCM background service when you try to kill the app. Also this happens only if i configure foregroundNotificationConfig

Well the problem is that the app does not get killed and somehow the geolocator attach itself to something else to keep it self alive.

robpot95 avatar Jan 08 '24 19:01 robpot95

Thank you for reporting this issue, I have labelled it as a bug and added it to our backlog.

mvanbeusekom avatar Jan 15 '24 10:01 mvanbeusekom

This happens because we do flutter engine reference counting to keep the location service alive when location is used in conjunction with another package like flutter_background so that location service can be used in Android Services and not be dependent on an Activity that can be killed by Android. My suggestion would be that you listen to the activity lifecycle in Flutter and cancel your location stream when the activity is killed.

Wackymax avatar Jan 24 '24 06:01 Wackymax

Flutter's lifecycle management detached is never called on android when the app is killed by the user, same as dispose() as mentioned here (https://github.com/flutter/flutter/issues/40940).

This causes the message "running in background" still showing when the app is terminated by the user and it stays forever unless user force stops the app. Can we use MainActivity.onDestroy on the native side to stop geolocator and if so, how?

alaatm avatar Feb 04 '24 13:02 alaatm

Hi all,

I was looking into this and I noticed that the geolocator keeps hold of the FlutterPluginBinding instance after the onDetachedFromActivity method is being called. In the permission_handler plugin this resulted into leaking the activity which I think might also have an impact on this issue.

Unfortunately this is a bit difficult for me to test, is someone willing to verify if this issue still occurs when using version 4.5.2 of the geolocator_android package?

mvanbeusekom avatar Feb 07 '24 10:02 mvanbeusekom

I doubt that is what's causing this. It would be the reference counting of the active flutter engines doing this.One possible way to solve this would be to keep a smarter reference knowing about the instance of the service using geolocator rather than just plain service counting.On 07 Feb 2024, at 12:36, Maurits van Beusekom @.***> wrote: Hi all, I was looking into this and I noticed that the geolocator keeps hold of the FlutterPluginBinding instance after the onDetachedFromActivity method is being called. In the permission_handler plugin this resulted into leaking the activity which I think might also have an impact on this issue. Unfortunately this is a bit difficult for me to test, is someone willing to verify if this issue still occurs when using version 4.5.2 of the geolocator_android package?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

Wackymax avatar Feb 07 '24 12:02 Wackymax

I've tried updating the geolocator_android package to version 4.5.2 in my environment, but unfortunately, I haven't noticed any changes regarding the issue.

hiromu-kon avatar Feb 21 '24 03:02 hiromu-kon

@mvanbeusekom I'm still facing challenges with the issue previously mentioned and I'm really in need of assistance. Despite updating the geolocator_android package to version 4.5.2 as suggested, I've observed no improvements concerning the problem. Our Android app users are currently left uncertain whether the location is being properly accessed, which is quite a critical concern for us.

Any insights or alternative solutions to circumvent this issue would be immensely appreciated. We're eager to resolve this as smoothly and swiftly as possible for the benefit of our users.

hiromu-kon avatar Mar 11 '24 00:03 hiromu-kon

I think there is a misunderstanding here. If you want the location services to stop when the application is killed then do not configure a foreground notification. The intention of that is to increase the priority of the flutter activity to try and not have it killed. If you do need the location in the background then use a background service plugin that will start a separate background service. Otherwise, remove the foreground notification config so that the location service can shut down when the activity is killed.

Wackymax avatar Mar 22 '24 12:03 Wackymax

@Wackymax

In our app we need to track location updates when the app is running (whether foreground or minimzed). For that, we are utilizing the foreground service as I understand from the android docs that this is the way to do it (https://codelabs.developers.google.com/codelabs/while-in-use-location/#2) under Services, foreground services, and binding section. We do not required location tracking when the app is killed so from my understanding we should not declarate the background location permission as this requires extra approval from Google.

When the app is in foreground and the user swipes it away (terminates it) the location service is stopped. However, when it is minimzed and the user swipes it away the location service is still on and the location notification still persists for the user.

Edit: When location service is started, swiping the app away (regardless whether it was foreground or minimized) keeps the location notification on. The log from geolocator says: There is still another flutter engine connected, not stopping location service.

As I mentioned previously, we cannot rely on lifecycle events to stop the service on terminate events because these events are not raised when the app is terminated.

alaatm avatar Mar 22 '24 19:03 alaatm

You can still use the flutter background plugin to accomplished this. It will be a lot more reliable than just using the foreground notification built in with this package.On 22 Mar 2024, at 22:00, Alaa Masoud @.***> wrote: @Wackymax In our app we need to track location updates when the app is running (whether foreground or minimzed). For that, we are utilizing the foreground service as I understand from the android docs that this is the way to do it (https://codelabs.developers.google.com/codelabs/while-in-use-location/#2) under Services, foreground services, and binding section. We do not required location tracking when the app is killed so from my understanding we should not declarate the background location permission as this requires extra approval from Google. When the app is in foreground and the user swipes it away (terminates it) the location service is stopped. However, when it is minimzed and the user swipes it away the location service is still on and the location notification still persists for the user.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

Wackymax avatar Mar 22 '24 20:03 Wackymax

I just read about stopWithTask flag in the android docs with the following comment:

If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities

Maybe if this is defined in the manifest file as follows:

<service
                android:enabled="true"
                android:exported="false"
                android:foregroundServiceType="location"
+               android:stopWithTask="true"
                android:name=".GeolocatorLocationService"/>

It will stop the service when the app is killed?

This SO answer suggests the same.

alaatm avatar Mar 24 '24 21:03 alaatm

I just read about stopWithTask flag in the android docs with the following comment:

If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities

Maybe if this is defined in the manifest file as follows:

<service
                android:enabled="true"
                android:exported="false"
                android:foregroundServiceType="location"
+               android:stopWithTask="true"
                android:name=".GeolocatorLocationService"/>

It will stop the service when the app is killed?

This SO answer suggests the same.

so, is this working?

Tarun-kumar-koda avatar May 08 '24 08:05 Tarun-kumar-koda

We can't do this globally for all apps that use this library. If you don't need background location then don't pass in the foreground notification or override the geolocatoe service descriptor in the manifest to add this attribute for your app only.On 08 May 2024, at 10:08, Tarun-kumar-koda @.***> wrote:

I just read about stopWithTask flag in the android docs with the following comment: If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities Maybe if this is defined in the manifest file as follows: <service android:enabled="true" android:exported="false" android:foregroundServiceType="location"

  •           android:stopWithTask="true"
              android:name=".GeolocatorLocationService"/>
    

It will stop the service when the app is killed? This SO answer suggests the same.

so, is this working?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

Wackymax avatar May 08 '24 08:05 Wackymax

Thank you for reporting this issue, I have labelled it as a bug and added it to our backlog.

@mvanbeusekom As I see, there are 2 ways to support this:

  • set android:stopWithTask="true" to force stop when the user clears the app from background. Reference: https://developer.android.com/reference/android/R.attr#stopWithTask
  • write onTaskRemoved and add a flag (true/false) to android's notification settings to stop/not stop the service https://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent)

Is that correct? I can submit a pr. But what do you prefer?

vietstone-ng avatar Jul 01 '24 10:07 vietstone-ng

how can forcefully stop the service and clear foreground notification.

Suppose i want to stop tracking location from a button click.

mayank-vinove avatar Jul 02 '24 04:07 mayank-vinove

how can forcefully stop the service and clear foreground notification.

Suppose i want to stop tracking location from a button click.

Check this geolocator_android: git: url: https://github.com/hussenIbrahim/flutter-geolocator path: geolocator_android

hussenIbrahim avatar Jul 19 '24 08:07 hussenIbrahim

Unsubscribing from the position stream so that you don't listen to it anymore should do the trick.On 19 Jul 2024, at 10:41, Hussen Ibrahim @.***> wrote:

how can forcefully stop the service and clear foreground notification. Suppose i want to stop tracking location from a button click.

Check this geolocator_android: git: url: https://github.com/hussenIbrahim/flutter-geolocator path: geolocator_android

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

Wackymax avatar Jul 19 '24 09:07 Wackymax