-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Comments
@fernandofranca I am in the exact same situation. |
@hjh5696 Yup. In my environment I'd suggest you to wrap your call in a try/catch and check what kind of error is being thrown. |
@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? |
@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.
Don't forget: Do not use // inline comments |
@fernandofranca Thank you so much! I will let you know when I figure out my problem. |
@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. |
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:
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. |
My workaround:
add this to WebView injectedJavaScript prop |
@0x5e Confirm this hacking worked 😄 |
WebView really needs more love, on both iOS and Android sides apparently. |
Om it works!!! ;D
|
if (window.postMessage.length !== 1) { how to use that ?? |
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
The text was updated successfully, but these errors were encountered: