Flutter workmanager not working in ios background

439 Views Asked by At

I downloaded workmanager repository from Github. Then I added some code in the workmanager's callback dispatcher. I have included the code below. Then I debug using Xcode.

After triggering "Background Fetch" in Xcode, and minimizing the app - the workmanager callback executes - by showing a local notification. But when I close the app (terminate, remove from recent), it does not run in the background.

When the app is minimized, the app is working fine. But not working in the background.

I have tested with the release build.

What should I do right now?

This code I added inside the callback dispatcher

for (int i = 0; i < 1000; i++) {
      await Future.delayed(const Duration(seconds: 5));
      AwesomeNotifications().createNotification(
          content: NotificationContent(
              id: 10,
              channelKey: 'basic_channel',
              title: i.toString(),
              body: 'Workmanager '));
      print("background $i");
    }
    await Future.delayed(const Duration(minutes: 15));

I have initialized the notification package before this code.

1

There are 1 best solutions below

0
On

Don't expect it to work when the app is terminated (dismissed from the app switcher). registerOneOffTask() will work somewhat reliable only when the app is in the background.

The thing is, iOS decides when is a good time to dispatch your tasks from the queue. With factors like battery life, low power mode turned on, whether the screen is awake and even iOS's new ML models that learn from usage behavior - you can't reliably predict when your task will execute in the background.

You are issuing notifications from your callback dispatcher. Instead of using local notifications, you will have to configure a notification server to send remote notifications to your device.