Flutter Workmanager IOS oneofftask not working

201 views Asked by At

I'm trying to run a one-off task using the work manager plugin. Works fine on android, but on IOS I'm running into some issues.

I have set up as per the documentation here https://github.com/fluttercommunity/flutter_workmanager/blob/main/IOS_SETUP.md. The only part of this I'm unclear on is in my info.plist and app delegate I have to register the task. I have seen on some SO and GitHub threads such as here that I should be including my bundle ID before the task name. Should I be doing this in one, both or neither of these places.

I am running on an physical iPhone XR iOS 16.5.1 and am trying everything in both release and debug mode.

My appdelegate.swift (some values edited, function names edited but keep same format)

import UIKit
import Flutter
import Firebase
import GoogleMobileAds
import GoogleMaps
import workmanager

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
    GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ "testdeviceidhidden" ]
    GMSServices.provideAPIKey("apikeyhidden")
    UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
    WorkmanagerPlugin.registerTask(withIdentifier: "com.example.app-name.taskName1")
    WorkmanagerPlugin.registerTask(withIdentifier: "com.example.app-name.taskName2")
    WorkmanagerPlugin.registerTask(withIdentifier: "com.example.app-name.taskName3")

    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

project.pbxproj

// !$*UTF8*$!
{
    SystemCapabilities = {
        com.apple.BackgroundModes = {
            enabled = 1;
        };
    };
... rest of file below

info.plist

<key>BGTaskSchedulerPermittedIdentifiers</key>
    <array>
        <string>com.example.app-name.taskName1</string>
        <string>com.example.app-name.taskName2</string>
        <string>com.example.app-name.taskName3</string>
    </array>
<key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>processing</string>
    </array>

Dart code for tasks is laid out like this for all tasks. They all are one-off tasks and have either a delay of zero, 5 seconds or up to one hour.

Workmanager().registerOneOffTask(
        "taskName1", "taskName1",
        initialDelay: const Duration(seconds: 5),
        tag: "taskName1",
        inputData: {"inputData": inputData});

Dart code for receiving tasks in main.dart

@pragma('vm:entry-point')
void callbackDispatcher() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  var messaging = FirebaseMessaging.instance;
  var analytics = FirebaseAnalytics.instance;

  Workmanager().executeTask((task, inputData) async {
    if (task == "taskName1") {
      //Do some things and return result
    }
  }
}

For the most part I get the error flutter: PlatformException(bgTaskSchedulingFailed(Error Domain=BGTaskSchedulerErrorDomain Code=3 "(null)") error, Scheduling the task using BGTaskScheduler has failed.

This implies that my set up is wrong, but I don't see where I have gone wrong. I tried a solution where I changed my pubspec.yaml from including work manager: ^0.5.2 to this

workmanager:
    git:
      url: https://github.com/absar/flutter_workmanager.git
      ref: ios-bg-tasks-enh-final

This removed the error however the tasks were not running, even after leaving the application running on a physical device overnight. I did get this error in that case

[BackgroundTask] Background Task 27 ("task name"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

As UIApplication.endBackgroundTask is swift (I believe) I have absolutely no clue how to call that through flutter.

Any help would be greatly appreciated. I can see plenty of threads with people having issues running work manager on IOS (and just general background tasks, which I understand IOS doesn't make easy) but I'm seeing very few solutions, and none so far that work for me.

0

There are 0 answers