Skip to content

🐞iOS: registerOneOffTask never registers with callbackDispatcher #450

Closed
@ChristopherBrislin

Description

@ChristopherBrislin
  • I have read the README
  • I have done the setup for Android
  • I have done the setup for iOS
  • I have ran the sample app and it does not work there

Version

Technology Version
Workmanager version 0.5.0
Xcode version 14.1
Swift version 5.7.1
iOS deployment target 11.0

Describe the error

Have followed all the directions in the iOS setup and guides but am unable to get the plugin to work correctly. No errors are thrown - the callback dispatcher is just never called. The code works without fault on Android. The test environment is a physical device (iPhone XS running iOS 16.1.2) and is being debugged from XCode. The Info.plist has been configured with the correct task indentifier and backgroundFetch seems to work as expected and triggers the callbackDispatcher, but registering a one off task doesn't.

callbackDispatcher as top level function:

void callbackDispatcher() {
  Workmanager().executeTask((taskName, inputData) {
    stderr.writeln(
        'Flutter VM, BACKGROUND TASK: Background task called! $taskName, extra data: $inputData');

    return Future.value(true);
  });
}

my AppDelegate.swift:

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

  override func userNotificationCenter(_ center: UNUserNotificationCenter,
                                         willPresent notification: UNNotification,
                                         withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
         completionHandler(.alert) // shows banner even if app is in foreground
     }
    
  

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      // This is required to make any communication available in the action isolate.
        UNUserNotificationCenter.current().delegate = self
        FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
          GeneratedPluginRegistrant.register(with: registry)
        }
        //WorkmanagerPlugin.register(with: self!.registrar(forPlugin: "com.v2software.onchocks.onchocks-background-logging"))
        WorkmanagerPlugin.registerTask(withIdentifier: "com.v2software.onchocks.onchocks-background-logging")
      
        UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
        GeneratedPluginRegistrant.register(with: self)
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
        }
      
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

I'm calling registerOneOffTask from a Popupmenuitem:

PopupMenuItem(
                              child: const Text('Record flight'),
                              onTap: () async {
                                print('Registering new task');
                                try {
                                  await Workmanager().registerOneOffTask(
                                      'com.v2software.onchocks.onchocks-background-logging',
                                      'com.v2software.onchocks.onchocks-background-logging',
                                      tag: 'ThisIsANewTag',
          
                                      inputData: {
                                        'user': user?.user.uid,
                                        'registeredEvent': true,
                                      },
                                      initialDelay: Duration.zero,
                                      existingWorkPolicy:
                                          ExistingWorkPolicy.replace);
                                } catch (err) {
                                  print(err.toString());
                                }
                              },
                            ),

When calling the onTap function this is the debug output:

2022-12-12 21:03:51.281401+1030 Runner[3948:242838] flutter: Registering new task
2022-12-12 21:04:17.589698+1030 Runner[3948:254976] [tcp] tcp_input [C2.1.1:3] flags=[R] seq=4218016391, ack=0, win=0 state=LAST_ACK rcv_nxt=4218016391, snd_una=1941703970
2022-12-12 21:04:17.592510+1030 Runner[3948:254976] [tcp] tcp_input [C2.1.1:3] flags=[R] seq=4218016391, ack=0, win=0 state=CLOSED rcv_nxt=4218016391, snd_una=1941703970
2022-12-12 21:04:17.593167+1030 Runner[3948:254976] [tcp] tcp_input [C2.1.1:3] flags=[R] seq=4218016391, ack=0, win=0 state=CLOSED rcv_nxt=4218016391, snd_una=1941703970

I noticed that the callback will fire at a very random time (not associated with any input or background fetch) and the task will be in the queue but missing the input data.

I was expecting behaviour similar to what I am seeing in Android where the callback function is triggered nearly immediately upon registering the task. Am I missunderstanding the functionality of this package or have I missused it?

**[✓] Flutter (Channel stable, 3.0.3, on macOS 13.0.1 22A400 darwin-x64, locale en-AU)
• Flutter version 3.0.3 at /Users/ChrisBrislin/Developer/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 676cefaaff (6 months ago), 2022-06-22 11:34:49 -0700
• Engine revision ffe7b86a1e
• Dart version 2.17.5
• DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /Users/ChrisBrislin/Library/Android/sdk
• Platform android-33, build-tools 33.0.0
• Java binary at: /Applications/Android
Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.3

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] IntelliJ IDEA Community Edition (version 2021.3.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.74.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.54.0

[✓] Connected device (4 available)
• Chris iphone (mobile) • 00008020-001870340E92002E • ios
• iOS 16.1.2 20B110
• iPhone 14 (mobile) • 91A06788-785D-40B3-AEA8-77AED473ECCF • ios
• com.apple.CoreSimulator.SimRuntime.iOS-16-1 (simulator)
• macOS (desktop) • macos • darwin-x64
• macOS 13.0.1 22A400 darwin-x64
• Chrome (web) • chrome • web-javascript
• Google Chrome 108.0.5359.98

[✓] HTTP Host Availability
• All required HTTP hosts are available

• No issues found!**

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions