在flutter中为ios初始化工作管理器

djmepvbi  于 2022-12-27  发布在  Flutter
关注(0)|答案(1)|浏览(205)

我正在尝试设置ios配置为文档https://github.com/fluttercommunity/flutter_workmanager/blob/main/IOS_SETUP.md这设置有什么问题我有这个错误:

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)

这是我在main中的代码//本地通知函数如何无效callbackDispatcher()async { final now = DateTime.now(); Workmanager().executeTask((task,inputData)async { prefs = await SharedPreferences.getInstance();如果(首选获取字符串(“随机时间”)!= null){最终检出时间=日期时间.解析(首选获取字符串(“随机时间”)!); if(checkouttime.isBefore(现在)){通知应用程序显示通知(正文:“人力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力物力”,标题:“我的应用”,id:1、有效载荷:“有效载荷”,);} }返回未来值(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",
     );
  }

所以我正在尝试显示一个本地通知,如果时间的东西来了
它在Android上运行良好
这是我设置的ios文件
在信息列表中

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

这是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)
   }
  }

这是项目.pbxproj

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

5f0d552i1#

dart函数中registerOneOffTask()的第一个参数必须与swift中的withIdentifier相同,因此在您的情况下,您需要更改:

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

收件人:

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

注1:您需要使用带有bundle名称的string,而不是simpleTask,因此请将其更改为如下形式:com.example.project_name.simpleTask
注2:Workmanager插件当前在IOS设备上工作时存在问题,您可以在396402403中看到许多未决问题。

相关问题