Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native WebView onMessage doesn't work on Android #14586

Closed
fernandofranca opened this issue Jun 18, 2017 · 12 comments
Closed

React Native WebView onMessage doesn't work on Android #14586

fernandofranca opened this issue Jun 18, 2017 · 12 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@fernandofranca
Copy link

fernandofranca commented Jun 18, 2017

Description

WebView's onMessage function never gets called on some Android versions.

This behaviour is not consistent on the Expo simulator. Test on physical devices.

Reproduction Steps

Se line 40 on the following snack.

If you test on a physical device and call window.postMessage without the second parameter it will throw "Failed to execute 'postMessage' on 'Window': 2 arguments required, but only 1 present".

The second parameter should be '*'.

See postMessage api on Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

postMessage seems to work only after the DOMContentLoaded event but not immediately. Expo simulator catches the two last events, but on a physical device I was able to trigger and receive messages before DOMContentLoaded. I think this should've been clarified on the docs.

Sample Code

https://snack.expo.io/HybU5stmW

Solution

Update the docs. Where is stated:
"window.postMessage accepts one argument, data, which will be available on the event object, event.nativeEvent.data. data must be a string."

Should state:
window.postMessage requires two arguments, data, ...., and targetOrigin which should be '*'.

Additional Information

  • React Native version: react-native: 0.44.2
  • Platform: Android 6.0 and 5.0 (didn't tested on previous versions)
  • Development Operating System: macOS 10.12.4
  • Build tools: Xcode 8.3.3, Android Studio 2.3
@fernandofranca fernandofranca changed the title React Native WebView onMessage doesn't work React Native WebView onMessage doesn't work on Android Jun 22, 2017
@hjh5696
Copy link

hjh5696 commented Jul 10, 2017

@fernandofranca I am in the exact same situation.
You mean window.postMessage (SOME_DATA, '*') should be used instead of window.postMessage (SOME_DATA). right?
But it does not work.. if I am doing something wrong, please let me know

@fernandofranca
Copy link
Author

@hjh5696 Yup.

In my environment window.postMessage(someString, '*'); works. YMMV as it's not documented and the behaviour is not consistent.

I'd suggest you to wrap your call in a try/catch and check what kind of error is being thrown.

@hjh5696
Copy link

hjh5696 commented Jul 10, 2017

@fernandofranca Thank you for reply. However, even if I use try/catch, how do I get an error message within the html code of webview? is it from Android studio?

@fernandofranca
Copy link
Author

@hjh5696 I didn't knew any other way and had to improvise.

Eg.: Inside your webview, use the catch block to add a new DOM element containing the stringified error.

function printStuff(stuff){
  var el = document.createElement("pre");
  var str = JSON.stringify(stuff);
  el.innerHTML = str;
  document.body.appendChild(el);
}

Don't forget: Do not use // inline comments

@hjh5696
Copy link

hjh5696 commented Jul 10, 2017

@fernandofranca Thank you so much! I will let you know when I figure out my problem.

@hjh5696
Copy link

hjh5696 commented Jul 10, 2017

@fernandofranca I solved my problem. In my case, it worked well in Android 7.1 but not in below versions. I used some arrow functions in my <script></script>, and that was the problem. After I changed them to function(){ }, it works well in below versions.
Thank you for your help!

@hramos
Copy link
Contributor

hramos commented Sep 21, 2017

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

@hramos hramos added the Icebox label Sep 21, 2017
@hramos hramos closed this as completed Sep 21, 2017
@0x5e
Copy link

0x5e commented Dec 14, 2017

My workaround:

if (window.postMessage.length !== 1) {
  window.postMessage = function(msg) {
    setTimeout(function () {
      console.log('window.postMessage not ready')
      window.postMessage(msg);
    }, 500);
  }
}

add this to WebView injectedJavaScript prop

@khanhtdbse
Copy link

@0x5e Confirm this hacking worked 😄
Remember to remove any comment, add missing semicolon in injectedJavaScript in order to work!

@fungilation
Copy link

WebView really needs more love, on both iOS and Android sides apparently.

@esipavicius
Copy link

Om it works!!! ;D

injectedJavaScript={" setTimeout(function(){ window.postMessage('asdsa', '*'); },1000); "} onMessage={ (event)=>{ let message = event.nativeEvent.data; console.log(message); console.log('message'); } }

@zabipakdel
Copy link

if (window.postMessage.length !== 1) {
window.postMessage = function(msg) {
setTimeout(function () {
console.log('window.postMessage not ready')
window.postMessage(msg);
}, 500);
}
}

how to use that ??
I have problem every one can help me??

@facebook facebook locked as resolved and limited conversation to collaborators Sep 21, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

9 participants