How can I keep background tasks alive when app goes to background in Android?

77 views
Skip to first unread message

Lucas Correa

unread,
Jan 15, 2021, 4:48:25 PM1/15/21
to Flutter Development (flutter-dev)

I have a bussness app that count time, set macros and show notification and alerts. I have a problem when app goes to background and Android stop my counting tasks.

I tried many things to keep these tasks alive, but I failed. This notification need to work offline, so FCM not a good solution.

How can I work arround it?

Obs.: When app goes to foreground, all tasks work and show all notifications... But I need to alert user just in time, not only in foreground.

Suzuki Tomohiro

unread,
Jan 15, 2021, 5:06:28 PM1/15/21
to Lucas Correa, Flutter Development (flutter-dev)
How about setting a notification to a specified time? You have to cancel it when necessary. This plugin does not require the Internet access:

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/baa71d7d-87f7-4fd8-b9f1-3fec9b8eaa16n%40googlegroups.com.

Lucas Correa

unread,
Jan 15, 2021, 5:44:04 PM1/15/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
I use this plugin to generate notification... but notification depends which time counting

ex.: if user stay 5 minutes on "macro X", show this notification
if user does not move for 2 minutes long in "macro Y", show other notification.

Android are stopping counting tasks and I'm not able to show notification.
--
Lucas Correa

Lucas Correa

unread,
Jan 19, 2021, 8:42:46 AM1/19/21
to Flutter Development (flutter-dev)

https://stackoverflow.com/questions/65743792/how-can-i-keep-background-tasks-alive-in-background-on-android-using-flutter/65744078#65744078



I founded a solution!

Searching the site https://dontkillmyapp.com/ I saw many apps with the same problem and some solution ideas for differets brands and models.

After checking some of them, I saw that many installed apps has this config for default, so I search how can I do it programactlly.

Here the solution:

pubspec.yaml

android_power_manager: ^0.1.6 permission_handler: ^5.0.1+1

Function:

void init() async { var status = await Permission.ignoreBatteryOptimizations.status; print("status: $status"); if (status.isGranted) { print( "isIgnoring: ${(await AndroidPowerManager.isIgnoringBatteryOptimizations)}"); if (!(await AndroidPowerManager.isIgnoringBatteryOptimizations)) { AndroidPowerManager.requestIgnoreBatteryOptimizations(); } } else { Map<Permission, PermissionStatus> statuses = await [ Permission.ignoreBatteryOptimizations, ].request(); print( "permission value: ${statuses[Permission.ignoreBatteryOptimizations]}"); if (statuses[Permission.ignoreBatteryOptimizations].isGranted) { AndroidPowerManager.requestIgnoreBatteryOptimizations(); } else { exit(0); } } }

AppWidget.dart (main)

@override Widget build(BuildContext context) { init(); SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); DataTransferService().initTimerTask(); return MaterialApp( navigatorKey: Modular.navigatorKey, title: APP_NAME, theme: ThemeData( primarySwatch: Colors.green, ), initialRoute: '/', routes: { '/': (context) => LoginPage(), '/home': (context) => HomePage(), '/notification': (context) => NotificationPage(), '/alerts': (context) => AlertsPage(), }, onGenerateRoute: Modular.generateRoute, ); }

So, the app ask permission (if needed) and, if permission is granted, ask user to set app to ignore battery optimization.

Now the notifications are working all rigt! =]

Reply all
Reply to author
Forward
0 new messages