I can't get React Native WebView postMessage to work. The React Native app successfully receives the post message sendt from the web app. But the web app does not receive messages sendt from the React Native app.
My simple web app
<html> <body> <button>Send post message from web</button> <div>Post message log</div> <textarea style="height: 50%; width: 100%;" readonly></textarea> <script> var log = document.querySelector("textarea"); document.querySelector("button").onclick = function() { console.log("Send post message"); logMessage("Sending post message from web.."); window.postMessage("Post message from web", "*"); } window.addEventListener("message", function(event) { console.log("Received post message", event); logMessage(event.data); }, false); function logMessage(message) { log.append((new Date()) + " " + message + " "); } </script> </body> </html>
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", ...
1.4m articles
1.4m replys
5 comments
57.0k users