Android window.ReactNativeWebView.postMessage issue

See original GitHub issue

Hi there,

We implemented version 5.0.5 of the library with #243 incorporated into it using window.ReactNativeWebView.postMessage for bidirectional communication. We use both iOS and Android with “react-native”: “0.57.0”.

The problem we see is the following: iOS => web view => ✅ (use injectJavaScript to send a message) web view => iOS => ✅ (use window.ReactNativeWebView.postMessage to send a message) Android => web view => ✅ (use injectJavaScript to send a message) web view => Android => 👎 (use window.ReactNativeWebView.postMessage to send a message)

Android errors with:

java bridge method can't be invoked on a non injected object.

This feels somewhat weird, since the whole java bridge is supposed to be managed for us, as long as messagingEnabled=true, which is computed from messagingEnabled={typeof this.props.onMessage === 'function'}. Our WebView has onMessage implemented with a function, so that all should be good, and is also “confirmed” by the fact that iOS works as expected.

Am I missing something obvious, or is this a bug?

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:23

github_iconTop GitHub Comments

50reactions
laukaichungcommented, Feb 21, 2019

@Titozzz I didn’t realize that the targetOrigin argument must be omitted in window.ReactNativeWebView.postMessage().

If the targetOrigin(*) is added, it will throw the method does not exist error!

// This one won't work.
window.ReactNativeWebView.postMessage(JSON.stringify(data), "*");

// This will work.
window.ReactNativeWebView.postMessage(JSON.stringify(data));

36reactions
guavadevelopmentcommented, Jul 16, 2019

I just had the same error and narrowed it down in my case to a javascript binding issue. You can easily reproduce the java bridge method can't be invoked on a non injected object error by assigning the window.ReactNativeWebView.postMessage to a local variable then trying to invoke that:

let postMessageProxy = window.ReactNativeWebView.postMessage;
postMessageProxy('hey....') //boom :(

By assigning to a local variable and calling that, javascript is changing the functions this and the Android WebView appears to be quite unhappy with that. It is quite easy to work around by ensuring the binding is preserved:

let postMessageProxy = function(data) { window.ReactNativeWebView.postMessage(data); }
postMessageProxy('hey....') //yay :)

As a real world example and the reason I came across this was for some code which was shared between hosting in a WebView or an iframe and I was trying to do this:

//find a postMessage to talk to hoster
let postMessage = window.parent.postMessage;
if(window.ReactNativeWebView) {
    postMessage = window.ReactNativeWebView.postMessage;
}

postMessage('hey');
Read more comments on GitHub >

github_iconTop Results From Across the Web

window.ReactNativeWebView.postMessage not working in ...
I tried window.ReactNativeWebView.postMessage("hello") in React JS, but the onMessage is not being invoked in React Native Webview.
Read more >
How to communicate with data between WebViews in React ...
In the “script” tag, we are going to call a sendDataToReactNativeApp() function in which we will use a window property called window.ReactNativeWebView.
Read more >
window.reactnativewebview.postmessage android
Send message from react native webviewRef.current.injectJavaScript(JSON.stringify({msg: "The message here"})) // Recive message from webview document.
Read more >
Fixing React Native WebView's postMessage for iOS
In 2016, a GitHub user opened an issue on the React Native repository reporting the error, but nothing has been done to resolve...
Read more >
react-native-webview - npm
React Native WebView component for iOS, Android, macOS, and Windows. ... If any step seems unclear, please create a detailed issue.
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