Flutter: How to receive post message communication from outer app?

Not 100% sure if this will be helpful but try Interactive Webview. Using the didReceiveMessage.listen() method you should be able to listen to messages sent to the native webview.


With dart:html library we can establish communication channel between flutter web app and its host android WebView:

import 'dart:html' as html;

//...

    html.window.onMessage.listen((event) {

      // do something with received data
      myController.text = event.data; 

      // return a response
      event.ports[0].postMessage("response data");
    });

Corresponding Kotlin code from android side:

    val webView = findViewById<WebView>(R.id.webview)
    val (receiver, sender) = webView.createWebMessageChannel()
    receiver.setWebMessageCallback(object : WebMessagePort.WebMessageCallback() {
        override fun onMessage(port: WebMessagePort, message: WebMessage) {
            Log.i("example", "Received ${message.data}")
        }
    })
    val message = WebMessage("Original message", arrayOf(sender))
    webView.postWebMessage(message, Uri.EMPTY)