Flutter IOS Workmanager Background Fetch Never Called in Production

Issue

I’m seemingly unable to get background fetch working with Flutter workmanager on IOS.

I can confirm that it is working when called within xcode through debug. Just never when deployed to the device.

I’ve got my workmanager initialised and callback setup in main.dart

    ...

    Workmanager().initialize(
        callbackDispatcher,
        isInDebugMode: true
    );
}

void callbackDispatcher() 
{
  Workmanager().executeTask((task, inputData) async 
  {
    function()
    
    return Future.value(true);
  });
}

I’ve added fetch background mode to info.plist

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
</array>

I’ve added system capabilities to project.pbxproj

SystemCapabilities = {
    com.apple.BackgroundModes = {
        enabled = 1;
    };
};

I’ve added the plugin to appdelegate.swift

import UIKit
import Flutter
import workmanager

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {

    UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

Be much appreciated if anyone has a working setup.

Solution

Ok. For future people reading this with the same issue, this does ‘eventually‘ work.

After having the app open in the background for ~48hrs, I finally got a background task to run. This is by no means a complete success as it is not really at the frequency desired, but this implementation does ‘work‘ none the less.

Just run in debug mode to confirm and be very patient.

Answered By – BadgerHobbs

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *