Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞Tasks do not run on iOS at all #521

Open
4 tasks done
SticksDev opened this issue Nov 10, 2023 · 9 comments
Open
4 tasks done

🐞Tasks do not run on iOS at all #521

SticksDev opened this issue Nov 10, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@SticksDev
Copy link

  • 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 latest
Xcode version 15.0.1
Swift version latest
iOS deployment target 13

Describe the error
I have followed all of the setup steps, run the command in the debugger, and nothing happens. No errors, just the task does not get triggered at all. I both have the @pragma('vm:entry-point') on my task handler and listen for both the name and the iosbackroundtask enum. My initial print doesn't even get logged either...

Ideas? Is This a possible bug? Thanks.

Output of flutter doctor -v


[✓] Flutter (Channel stable, 3.13.9, on macOS 14.1.1 23B81 darwin-x64, locale en-US)
    • Flutter version 3.13.9 on channel stable at /usr/local/Caskroom/flutter/3.13.9/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d211f42860 (2 weeks ago), 2023-10-25 13:42:25 -0700
    • Engine revision 0545f8705d
    • Dart version 3.1.5
    • DevTools version 2.25.0

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.14.2

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
    • IntelliJ at /Users/sticks/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 76.3.4
    • Dart plugin version 232.10072.19

[✓] Connected device (2 available)
    • [omitted] iPhone (mobile) • 00008120-001A698A01E3C01E • ios        • iOS 17.0.3 21A360
    • macOS (desktop)          • macos                     • darwin-x64 • macOS 14.1.1 23B81 darwin-x64

[✓] Network resources
    • All expected network resources are available.```
@SticksDev SticksDev added the bug Something isn't working label Nov 10, 2023
@DavidIjsud
Copy link

DavidIjsud commented Nov 16, 2023

@SticksDev are you still getting the error? if so, then what other versions of ios did you test it?

@SticksDev
Copy link
Author

iOS 17.0.3 and I used your demo app, which also did not work.

@project4-0
Copy link

Hello I have the same problem. Background task are running on IOS only if Workmanager is initialized in debug mode = true. Workmanager().initialize(callbackDispatcher, isInDebugMode: true); If isInDebugMode = false. Does not start.

@project4-0
Copy link

I do register the oneOffTask with static method like this. On Android works. but on IOS not.
static void registerOneOffTask(
{required String taskKey,
required String taskName,
required Map<String, dynamic> inputData,
required Duration initialDelay}) {
try {
Constraints constraints = Constraints(
networkType: NetworkType.not_required,
requiresBatteryNotLow: false,
requiresCharging: false,
requiresDeviceIdle: false,
requiresStorageNotLow: false,
);
if(Platform.isIOS){
Workmanager().registerOneOffTask(taskKey, taskName,
initialDelay: initialDelay,constraints: constraints, inputData: inputData, existingWorkPolicy: ExistingWorkPolicy.append);
}else {
Workmanager().registerOneOffTask(taskKey, taskName,
initialDelay: initialDelay, inputData: inputData, existingWorkPolicy: ExistingWorkPolicy.append);
}
if (kDebugMode) {
DateTime executionTime = DateTime.now().add(initialDelay);
print('Task $taskKey will be executed at $executionTime');
}
} catch (e, s) {
if (kDebugMode) {
print('Error in registerOneOffTask');
print(e);
print(s);
}
}
}

@SticksDev
Copy link
Author

Any updates?

@brunodmn
Copy link

brunodmn commented Dec 7, 2023

I am not able to use it as well. I follow documentation in details, I even created a new project just to test this package only.

BGTaskScheduler works if I triggered by e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"task-identifier"], not on function call on flutter.

Background Fetch won't work or throw any erros (trying to trigger it by Xcode>debug>Simulate Background Fetch)

I have:

  • added Background Modes capability (Background processing and background fetch)
  • registered my task on AppDelegate.swift and Info.plist

background_service.dart:

import 'dart:io';

import 'package:workmanager/workmanager.dart';

@pragma(
    'vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    switch (task) {
      case Workmanager.iOSBackgroundTask:
        stderr.writeln("The iOS background fetch was triggered");
        print(
            "The iOS background fetch was triggered"); //simpleTask will be emitted here.
        break;
      default:
        print(
            "Native called background task: $task"); //simpleTask will be emitted here.
        break;
    }

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

class BackgruondService {
  static Future<void> initialize() async {
    Workmanager().initialize(
        callbackDispatcher, // The top level function, aka callbackDispatcher
        isInDebugMode:
            true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
        );
    Workmanager().registerOneOffTask("task-identifier", "simpleTask");
  }
}

AppDelegate.swift:

import UIKit
import Flutter
import workmanager

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        // WORKMANAGER
        UNUserNotificationCenter.current().delegate = self
        WorkmanagerPlugin.setPluginRegistrantCallback { registry in
            // Registry in this case is the FlutterEngine that is created in Workmanager's
            // performFetchWithCompletionHandler or BGAppRefreshTask.
            // This will make other plugins available during a background operation.
            GeneratedPluginRegistrant.register(with: registry)
        }
        WorkmanagerPlugin.registerTask(withIdentifier: "task-identifier")
        // background fetch
        UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
        // WORKMANAGER
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

Info.plist:

(...)
        <key>BGTaskSchedulerPermittedIdentifiers</key>
	<array>
		<string>task-identifier</string>
	</array>
	<key>UIBackgroundModes</key>
	<array>
		<string>fetch</string>
		<string>processing</string>
	</array>
(...)

Am I missing something? Documentation does not seem quite up to date...

@DominicGBauer
Copy link

I moved to this PR and it seems to be working for me now #511

@joonne
Copy link

joonne commented May 10, 2024

Commenting now on multiple issues while debugging the issue with plugins on iOS background isolates, but it finally seems that the missing piece was this:

// AppDelegate.swift

GeneratedPluginRegistrant.register(with: self)
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
  GeneratedPluginRegistrant.register(with: registry)
}

With the above added (after GeneratedPluginRegistrant.register(with: self), I'm finally able to call SharedPreferences and also the Firebase.initializeApp() successfully 🎉

I did not find this documented anywhere, I'll try to find a suitable place from the official README.

@dsourav
Copy link

dsourav commented May 24, 2024

Commenting now on multiple issues while debugging the issue with plugins on iOS background isolates, but it finally seems that the missing piece was this:

// AppDelegate.swift

GeneratedPluginRegistrant.register(with: self)
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
  GeneratedPluginRegistrant.register(with: registry)
}

With the above added (after GeneratedPluginRegistrant.register(with: self), I'm finally able to call SharedPreferences and also the Firebase.initializeApp() successfully 🎉

I did not find this documented anywhere, I'll try to find a suitable place from the official README.

It worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants