androidflutterandroid-notificationsflutter-notificationflutter-background

Flutter_background plugin isn't displaying any notification


I am trying to keep my App running at all times in the background, as it needs to check a Webserver for certain kinds of updates every 5 minutes. I followed the documentation of the plugin (https://pub.dev/packages/flutter_background) but it still doesn't want to show me the (expected) notification, which results in the app being terminated when closed. I do get the "permission request" from the background service (the one that tells the user about how to disable background service etc.) and I also already asked for Notification Permissions before running the function and created the notification channel etc.

I added this: to my Android_Manifest.xml:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

And this is my Code which (should) activate background services:

_requestAlwaysOn() async {
    FlutterBackground.initialize();
    const androidConfig = FlutterBackgroundAndroidConfig(
      notificationTitle: "Manager läuft im Hintergrund",
      notificationText: "Wir empfangen die neusten Nachrichten :))",
      notificationImportance: AndroidNotificationImportance.High,
      notificationIcon: AndroidResource(name: 'ic_dialog_info', defType: 'drawable'),
    );
    try {
      bool hasPermissions = await FlutterBackground.hasPermissions;
      bool enabled = FlutterBackground.isBackgroundExecutionEnabled;
      if(hasPermissions) {
        print(enabled);
        print(hasPermissions);
        bool success = await FlutterBackground.initialize(
            androidConfig: androidConfig);
        if (success) {
          print('background config successful');
          FlutterBackground.enableBackgroundExecution();
        } else {
          print('background config unsuccessful');
        }
      }
    } catch (e) {
      print('background config failed with error: $e');
    }
  }

Something that is interesting, is that I never got any feedback from the catch and that the enabled boolean prints as false on first try running the function and then as true when running it a second try.


Solution

  • Ok, I fixed it myself. Here is my Solution for everyone (maybe myself) in the future:

    Just add this:

    <service
            android:name="de.julianassmann.flutter_background.IsolateHolderService"
            android:exported="false" />
    

    inside the <application> tag in your AndroidManifest.xml