18.4 C
New York
Monday, March 10, 2025

Flutter: Workmanager Backgroundtast Registration failing in IOS


I’m growing an app with flutter which requires to have repeatedly notifications (utilizing local_notifications package deal). For Android the whole lot is working correctly with the code and the background activity is working with out a difficulty, independently whether or not the app is terminated or not.
For iOS I’m not in a position to register the backgroundtask efficiently. I’m at all times getting following suggestions in Xcode: Couldn't schedule BGAppRefreshTask The operation couldn’t be accomplished. (BGTaskSchedulerErrorDomain error 3.)

To this point I adopted the iOS setup instruction from the ´workmanager´ package deal:

  1. Including strains to AppDelegate.swift
  2. Including task-identifier to information.plist
  3. Register the background activity in essential.dart (working positive on android)

essential.dart:

@pragma(
    'vm:entry-point') // Necessary if the App is obfuscated or utilizing Flutter 3.1+
void callbackDispatcher() {
  Workmanager().executeTask((activity, inputData) async {
    print(
        "Native known as background activity: $activity"); //simpleTask can be emitted right here.
    attempt {
      if (await checkForJson()) {
        await displayNotificationWorkmanager();
      } else {
      }
      return Future.worth(true);
    } catch (err) {
      print(err.toString());
      return Future.worth(false);
      throw Exception(err);
    }
  });
}

void essential() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Workmanager().registerOneOffTask("task-identifier", "simpleTask");
  await flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
  );  
  Workmanager().initialize(
      callbackDispatcher, // The highest degree operate, aka callbackDispatcher
      isInDebugMode:
          true // If enabled it would submit a notification every time the duty is operating. Useful for debugging duties
      );
  Workmanager().registerPeriodicTask(
    "NAME_NOTIFICATION_TASK",
    "notification_task",
    initialDelay: Period(seconds: 10),
    frequency: Period(minutes: 15),
    existingWorkPolicy: ExistingWorkPolicy.substitute,
  );

  runApp(MyApp());

  // BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
}

Right here is the AppDelegate.swift

import UIKit
import Flutter
import workmanager
import flutter_local_notifications


@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
 override func userNotificationCenter(_ middle: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
                  completionHandler([.alert, .badge, .sound])
  }

    override func utility(
        _ utility: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)        
        // In AppDelegate.utility technique
        // WorkmanagerPlugin.registerBGProcessingTask(withIdentifier: "notification_task")
        
        // Register a periodic activity in iOS 13+
        WorkmanagerPlugin.registerPeriodicTask(withIdentifier: "notification_task", frequency: NSNumber(worth: 20 * 60))

        // FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
        // GeneratedPluginRegistrant.register(with: registry)
        // }
        if #out there(iOS 10.0, *) {
            UNUserNotificationCenter.present().delegate = self as? UNUserNotificationCenterDelegate
        }
        return tremendous.utility(utility, didFinishLaunchingWithOptions: launchOptions)
    }

    override func utility(
        _ utility: UIApplication,
        performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
            WorkmanagerPlugin.setPluginRegistrantCallback{registry in GeneratedPluginRegistrant.register(with:registry)}
        completionHandler(.newData)
        }
    
}

A part of Data.plist:

BGTaskSchedulerPermittedIdentifiers
    
        notification_task
    
UIBackgroundModes
    
        fetch
        location
        processing
        remote-notification
    

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles