flutter_foreground_task icon indicating copy to clipboard operation
flutter_foreground_task copied to clipboard

Foreground Process Seems to be killed after a period of time

Open BJRichardsAZ opened this issue 3 years ago • 4 comments

We have implemented a pedometer package using isar database and flutter foreground task to allow a step counter to persist even if the user kills the app or hasn't opened it for some time. It's working fine initially, but after a few hours (we've noticed the biggest issue being overnight) the foreground service notification stays present, but we no longer see the data update in the foreground service notification. The app will no longer resume counting steps until the user reopens the app to start the process again, which causes some inconsistencies with the data when steps are counted.

BJRichardsAZ avatar May 03 '22 17:05 BJRichardsAZ

was it ios??? in my case, we periodically communicate with our server evert 5 minutes. It's working fine for 3 days and i keep on test. My question is, Does your app work periodically or when the event occurs, your app works?

Kimsangwon0509 avatar May 04 '22 01:05 Kimsangwon0509

This issue seems to be related to Doze mode on the Android platform.

When using the service for extended periods of time, it is recommended to turn off battery optimization by calling the FlutterForegroundTask.requestIgnoreBatteryOptimization() function.

Please let me know if the issue persists.

Dev-hwang avatar May 04 '22 06:05 Dev-hwang

We are currently ignoring battery optimizations and it is required before the foreground service will start. The interesting thing is that in the example app you sent over it doesn't require battery optimization to be ignored. It also doesn't stop functioning after leaving the app closed for elongated periods of time, which seems counter intuitive. I'm wondering if it might be related to where we are calling the WithForegroundTask widget, as the direct child of it in the example app is a scaffold, whereas in our app we are calling it and providing a class that returns a future builder as its child:

home: WithForegroundTask(
          child: FutureBuild(
              initialization: _initialization, inviteCode: inviteCode)),
 );

This future builder is just waiting on firebase to initialize before loading the main UI of the application, but this is the only place where the setups seem to differ in a larger way. Let me know if you have any ideas.

BJRichardsAZ avatar May 04 '22 16:05 BJRichardsAZ

It seems to be fine on real devices with Android 9.0 and lower. Can I know the development environment and the version of the device you tested?

I will continue to track down the issue.

Dev-hwang avatar May 13 '22 04:05 Dev-hwang

This might also help https://stackoverflow.com/questions/67039618/getting-location-updates-in-foreground-service-isnt-working-when-app-is-moved-t

Faiizii avatar May 15 '22 17:05 Faiizii