question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

posting a message to webview doesnt work

See original GitHub issue

Hi ! I was sending a message to my WebView like this :

    this.myWebView.postMessage(
      JSON.stringify({
        messageType: "handleServerResponse",
        responseData: json,
        requestId
      })
    );

but after the upgrade it has stopped working, and now i’m trying to use the injectJavaScript instead.

This is what i am trying to do :

    const injectedJS= `
      window.postMessage("${JSON.stringify({
        messageType: "handleServerResponse",
        responseData: json,
        requestId
      })}"); 
    true;`
   this.myWebView.injectJavaScript(injectedJS)

but the message is not received on the WebView side, communication from the WebView to the app it self by using window.ReactNativeWebView.postMessage(data); is working fine.

please help

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
adophpcommented, Aug 5, 2019

@MaxToyberman

https://stackoverflow.com/questions/41160221/react-native-webview-postmessage-does-not-work#

document.addEventListener("message", ... and window.addEventListener("message",    

i used

window.addEventListener("message", function(){});

is worked.

2reactions
MaxToybermancommented, Jul 25, 2019

ok, i have found a solution but i am not sure this is the best way.

    this. myWebView.injectJavaScript(`
    (function(){
      window.postMessage('${data}','*');
    })();
    true;
    `)
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native WebView postMessage does not work
I can't get React Native WebView postMessage to work. The React Native app successfully receives the post message sendt from the web app....
Read more >
Window.postMessage() - Web APIs | MDN
The window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it ...
Read more >
Fixing React Native WebView's postMessage for iOS
The rn-webview package works by directing window.postMessage traffic to history.pushState instead. While React Native's iOS implementation cannot handle ...
Read more >
React Native WebView: A complete guide - LogRocket Blog
In this post, we are going to look at the most common use cases of React Native WebView, including: Getting started; Creating the...
Read more >
WebView - React Native
mixedContentMode ​ · never (default) - WebView will not allow a secure origin to load content from an insecure origin. · always -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found