React Native WebView postMessage does not work

Add browser specific event listeners

In Android

 document.addEventListener("message", function(event) {
       alert("This is a Entry Point Working in Android");
       init(event.data)
  });

In iOS

 window.addEventListener("message", function(event) {
       alert("This is a Entry Point Working in iOS");
       init(event.data)
  });

enter image description here


Version 5 of react-native-webview changes how this behavior works. You now want:

window.ReactNativeWebView.postMessage(data);

I had the same problem and fixed it by adding the event listener to the document instead of the window. Change:

window.addEventListener("message", ...

to:

document.addEventListener("message", ...