Reputation: 26
I am developing an app with flutter which requires to have repeatedly notifications (using local_notifications
package). For Android everything is working properly with the code and the background task is working without an issue, independently whether the app is terminated or not.
For iOS I am not able to register the backgroundtask successfully. I am always getting following feedback in Xcode: Could not schedule BGAppRefreshTask The operation couldn’t be completed. (BGTaskSchedulerErrorDomain error 3.)
So far I followed the iOS setup instruction from the ´workmanager´ package:
main.dart:
@pragma(
'vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
print(
"Native called background task: $task"); //simpleTask will be emitted here.
try {
if (await checkForJson()) {
await displayNotificationWorkmanager();
} else {
}
return Future.value(true);
} catch (err) {
print(err.toString());
return Future.value(false);
throw Exception(err);
}
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Workmanager().registerOneOffTask("task-identifier", "simpleTask");
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
);
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().registerPeriodicTask(
"NAME_NOTIFICATION_TASK",
"notification_task",
initialDelay: Duration(seconds: 10),
frequency: Duration(minutes: 15),
existingWorkPolicy: ExistingWorkPolicy.replace,
);
runApp(MyApp());
// BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
}
Here is the AppDelegate.swift
import UIKit
import Flutter
import workmanager
import flutter_local_notifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// In AppDelegate.application method
// WorkmanagerPlugin.registerBGProcessingTask(withIdentifier: "notification_task")
// Register a periodic task in iOS 13+
WorkmanagerPlugin.registerPeriodicTask(withIdentifier: "notification_task", frequency: NSNumber(value: 20 * 60))
// FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
// GeneratedPluginRegistrant.register(with: registry)
// }
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(
_ application: UIApplication,
performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
WorkmanagerPlugin.setPluginRegistrantCallback{registry in GeneratedPluginRegistrant.register(with:registry)}
completionHandler(.newData)
}
}
Part of Info.plist:
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>notification_task</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
</array>
Upvotes: 1
Views: 114