How do I keep background tasks running when the app goes to the background on Flutter Android?

Asked

Viewed 186 times

1

I have an application for a (specific) company that has some tasks for the purpose of counting time, marking macros and generating notifications (electronic point type).

When the app gets too long in the background, sometimes android freezes the tasks and the app can’t generate the notifications at the time it should occur. When the app comes back to the foreground, td fixes itself and notifications are shown retroactively.

How to get around this situation?


I found a viable solution!

Analyzing the site https://dontkillmyapp.com/ I noticed that many also go through this same problem, besides seeing some solutions ideas. After comparing the solutions to the different manufacturers, I saw that some apps that have this notification in the background already have this qnd configuration are installed.

Searching how to "force" this setting, I arrived at the following 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,
    );
  }

With this, it asks for permission (if necessary) and, if you have permission, already generates the popup for the user to allow the app to be exception in battery optimization.

Now the notifications are coming straight! =]

  • I have bad news for you, the control is arbitrary and depends on manufacturers. Each implements measures that end up killing apps and preventing them from running in the background. So take a look at https://dontkillmyapp.com/

  • This is something that the operating system defines and controls, there is in my view these task should be orchestrated by your backend, example when a taks were defned your backend would send a push and when opening the application, your taks syncronize with the back again.

  • yes... I am doing this... but there are some situations where I need to alert the user to take some action in the app, msm in these situations.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.