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.
--
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.
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+1Function:
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! =]