fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
847 stars 256 forks source link

How to register method channel listener in native side #412

Open LangInteger opened 2 years ago

LangInteger commented 2 years ago

Hi, I want to call the method channel on iOS side in the background job, but I got the following error when I tried to do so:

flutter: Exception happen: MissingPluginException(No implementation found for method echo on channel com.foo.bar)

Declaring the method channel in AppDelegate.swift:

      //=======================================================================================
      //====================  iOS native side custom method channel callee  ===================
      //=======================================================================================
      print("setup channel")
      let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
      let channel = FlutterMethodChannel(name: "com.foo.bar", binaryMessenger: controller.binaryMessenger)
      channel.setMethodCallHandler({
        (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
          if (call.method == "echo") {
              result(call.arguments as? String);
          } else {
              result("Not Implemented")
          }
      })

Calling method channel from callbackDispatcher with:

  final channel = MethodChannel("com.foo.bar");
  await channel.invokeMethod<String>("echo", "${DateTime.now()}");

Any suggestions on this situation?

kitoheedong commented 2 years ago

Hi, I want to call the method channel on iOS side in the background job, but I got the following error when I tried to do so:

flutter: Exception happen: MissingPluginException(No implementation found for method echo on channel com.foo.bar)

Declaring the method channel in AppDelegate.swift:

      //=======================================================================================
      //====================  iOS native side custom method channel callee  ===================
      //=======================================================================================
      print("setup channel")
      let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
      let channel = FlutterMethodChannel(name: "com.foo.bar", binaryMessenger: controller.binaryMessenger)
      channel.setMethodCallHandler({
        (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
          if (call.method == "echo") {
              result(call.arguments as? String);
          } else {
              result("Not Implemented")
          }
      })

Calling method channel from callbackDispatcher with:

  final channel = MethodChannel("com.foo.bar");
  await channel.invokeMethod<String>("echo", "${DateTime.now()}");

Any suggestions on this situation?

Did you solve the problem?

LangInteger commented 2 years ago

@heedongrichrich not yet.

Nitingadhiya commented 2 years ago

Same issue facing here @LangInteger @heedongrichrich

kitoheedong commented 2 years ago

@Nitingadhiya Man, I just decided to implement by myself hahaha~ It's a bit way better to implement background dispatch since there is no "one size fits all strategies" at this moment.

Cmk427 commented 4 months ago

any update?