MissingPluginException(在通道 Awesome_notifications 上未找到方法 createNewNotification 的实现)

问题描述 投票:0回答:1

每当使用 Workmanager 插件成功完成后台任务时,我都会尝试使用 Awesome 通知插件发送通知。让 Workmanager 在 iOS 上工作是一件痛苦的事情,但经过几天的调试,它终于成功了。当我在 xcode 上执行“模拟后台获取”时,我收到“执行获取已完成”的通知。它按预期工作,每 15 分钟一次。现在,问题出在 Awesome 通知插件上。我已按照here中的 iOS 设置进行操作。这是我的 AppDelegate.swift

SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
              SwiftAwesomeNotificationsPlugin.register(
                with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
              SharedPreferencesPlugin.register( // replace here
                                with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
          }

    // Register background tasks only if not already registered
    if !AppDelegate.hasRegisteredBackgroundTask {
        WorkmanagerPlugin.registerBGProcessingTask(withIdentifier: "iOSBackgroundProcessing")

        // Register a periodic task in iOS 13+
        WorkmanagerPlugin.registerPeriodicTask(
            withIdentifier: "locationTracking",
            frequency: NSNumber(value: 15 * 60)
        )

        AppDelegate.hasRegisteredBackgroundTask = true
    }

这就是我的代码在 main 中的样子

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  AwesomeNotifications().initialize(
    // set the icon to null if you want to use the default app icon
      null,
      [
        NotificationChannel(
            channelGroupKey: 'basic_channel_group',
            channelKey: 'basic_channel',
            channelName: 'Basic notifications',
            channelDescription: 'Notification channel for basic tests',
            defaultColor: Color(0xFF9D50DD),
            ledColor: Colors.white)
      ],
      // Channel groups are only visual and are not required
      channelGroups: [
        NotificationChannelGroup(
            channelGroupKey: 'basic_channel_group',
            channelGroupName: 'Basic group')
      ],
      debug: true
  );


  // Only after at least the action method is set, the notification events are delivered
  AwesomeNotifications().setListeners(
      onActionReceivedMethod:         NotificationController.onActionReceivedMethod,
      onNotificationCreatedMethod:    NotificationController.onNotificationCreatedMethod,
      onNotificationDisplayedMethod:  NotificationController.onNotificationDisplayedMethod,
      onDismissActionReceivedMethod:  NotificationController.onDismissActionReceivedMethod
  );

  // Ensure the notification is created on the main thread
  SchedulerBinding.instance.addPostFrameCallback((_) {
    AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: 10,
        channelKey: 'basic_channel',
        title: "Hello from WorkManager!",
        body: "This is a simple notification.",
      ),
    );
  });
  
  await Workmanager().cancelAll();
  await initializeWorkManager();
  Workmanager().registerPeriodicTask(
    "locationTracking", // Unique identifier for the task
    locationTracking, // Task name
    frequency: Duration(minutes: 15), 
  );
  Workmanager().printScheduledTasks();
  runApp(const MyApp());
}

Future<void> initializeWorkManager() async {
  await Workmanager().initialize(
    callbackDispatcher,
    isInDebugMode: true,
  );
  print("WorkManager initialization complete.");
}

@pragma('vm:entry-point')
void callbackDispatcher() {
  print("Workmanager task started.");
  Workmanager().executeTask((task, inputData) {
    switch(task){
      case locationTracking:
        print("Task excecuted: $task");
        AwesomeNotifications().createNotification(
          content: NotificationContent(
            id: 10,
            channelKey: 'basic_channel',
            title: "Hello from WorkManager!",
            body: "This is a simple notification triggered by the WorkManager task.",
          ),
        );
        print("Notification sent.");
    }
    return Future.value(true);
  });
}

有什么建议或想法如何修复它吗?我已经这样做太久了......

flutter background-task flutter-workmanager awesome-notifications
1个回答
0
投票

MissingPluginException 是添加严重依赖本机的包时的常见异常。尝试:

  1. 终止正在运行的应用程序
  2. 再次构建

如果没有帮助,请运行

flutter clean
flutter pub get
并重复该过程。如果 iOS 上仍然出现错误,请运行以下命令:

cd ios 
pod repo update
pod update --repo-update

并再次构建应用程序

© www.soinside.com 2019 - 2024. All rights reserved.