MikeNabil
MikeNabil

Reputation: 111

initialize workmanager in flutter for ios

i'm trying to set up ios configration as document https://github.com/fluttercommunity/flutter_workmanager/blob/main/IOS_SETUP.md what's the wrong with this set up i have this error:

Unhandled Exception: PlatformException(bgTaskSchedulingFailed(Error 
Domain=BGTaskSchedulerErrorDomain Code=3 "(null)") error, Scheduling the task using 
BGTaskScheduler has failed.

This may be due to too many tasks being scheduled but not run.

See the error details: Error Domain=BGTaskSchedulerErrorDomain Code=3 "(null)"., null, null)
#0      StandardMethodCodec.decodeEnvelope 
(package:flutter/src/services/message_codecs.dart:647:7)
#1      MethodChannel._invokeMethod 
(package:flutter/src/services/platform_channel.dart:294:18)
<asynchronous suspension>
#2      Workmanager.registerOneOffTask (package:workmanager/src/workmanager.dart:187:7)

and this is my code in main //how local notificaion function void callbackDispatcher() async { final now = DateTime.now(); Workmanager().executeTask((task, inputData) async { prefs = await SharedPreferences.getInstance(); if (prefs.getString('randomTime') != null) { final checkouttime = DateTime.parse(prefs.getString('randomTime')!); if (checkouttime.isBefore(now)) { NotificationApi.showNotification( body: "من فضلك قم بتاكيد تسجيلك", title: "My App", id: 1, payload: "payload", ); } } return Future.value(true); }); }

 void main() async {
   WidgetsFlutterBinding.ensureInitialized();
   NotificationApi.init();
   await GetStorage.init();
   final now = DateTime.now();
   prefs = await SharedPreferences.getInstance();
   //initialize workmanager
   await Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
   //start workmanager
   await Workmanager().registerOneOffTask(
           "1",
           "simpleTask",
     );
  }

so i'm trying to show a local notification if time of somethin is coming

it work fine with Android

and this is my set up for ios files

in info.plist

       <key>UIBackgroundModes</key>
       <array>
       <string>processing</string>
       </array>
       <key>BGTaskSchedulerPermittedIdentifiers</key>
       <array>
       <string>simpleTask</string>
       </array>

and this is 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) 
     WorkmanagerPlugin.registerTask(withIdentifier: "simpleTask")
     return super.application(application, didFinishLaunchingWithOptions: launchOptions)
   }
  }

and this is project.pbxproj

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

Upvotes: 8

Views: 3723

Answers (2)

C&#237;cero Moura
C&#237;cero Moura

Reputation: 2333

This problem happens only in simulator. It will work on real device.

Upvotes: 2

AnasSafi
AnasSafi

Reputation: 6294

First parameter of registerOneOffTask() in dart function must be same of withIdentifier in swift, so in your case you need to change:

 await Workmanager().registerOneOffTask(
           "1",
           "simpleTask",
     );

To:

 await Workmanager().registerOneOffTask(
           "simpleTask", // <<<< change this
           "simpleTask",
     );

Note 1: You need to use string with bundle name instead of simpleTask, so change it to something like : com.example.project_name.simpleTask

Note 2: Workmanager plugin has a current problem with work on IOS devices, you can see many opened issues here 396 , 402 , 403 .

Upvotes: 3

Related Questions