androidflutterwebview-flutter

Flutter WebView js channel does not work on android device


In my flutter project I use webView for 3d secure process. After the bank approve the page which opened in webView send some json data with js and I get this data by using js channel feature. All these process works well on ios device I can get json data. But in android device js channel cannot catch any message or maybe android device restrict js process on that page. I have no idea :))

Flutter 3.7.7 • channel stable

I used 2 version of webview_flutter package: 3.0.4 and 4.2.0 this problem not solved on any version.

My example code is below:

  late  final PlatformWebViewControllerCreationParams params;
     if (WebViewPlatform.instance is WebKitWebViewPlatform) {
       params = WebKitWebViewControllerCreationParams(
         allowsInlineMediaPlayback: true,
         mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
      );
    } else {
      params = const PlatformWebViewControllerCreationParams();
    }

    final WebViewController controller = WebViewController.fromPlatformCreationParams(params);

    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..loadHtmlString(html ?? "")
      ..addJavaScriptChannel(
        'Print',
        onMessageReceived: (JavaScriptMessage result) {
          final json = result.message.replaceAll('message: craftgateResponse,', '"message": "craftgateResponse",').replaceAll('value', '"value"');

          final data = jsonDecode(json) as Map<String, dynamic>;

          AppCore.pop(
            context: context,
            pop: CreditCardPaymentResponse.fromJson(data),
          );
        },
      );

    if (controller.platform is AndroidWebViewController) {
      AndroidWebViewController.enableDebugging(true);
      (controller.platform as AndroidWebViewController).setMediaPlaybackRequiresUserGesture(false);
    }`

Have you encountered this problem before? Any idea for solution of this problem?

Thank you all guys from now.


Solution

  • Update for those who encounter a similar issue in the future: This error was encountered in both debug and release modes. I found the cause of the error and have solved it now. The error was due to the js post line on the web page. window.JsChannel.postMessage('sample json response' , '*' ); The '*' part in this line prevents the capture of the message by android as I don't understand why, removing the '*' part solved the problem.