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.

React Native WebView onMessage doesn't work on Android

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
0x5ecommented, 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

10reactions
hjh5696commented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebView onMessage is not working in Android. #422 - GitHub
"react-native-webview": "^5.3.1". Description : WebView onMessage doesn't work on Android. export default class TestContainer extends Component ...
Read more >
React Native WebView onMessage() is not triggered
React Native WebView onMessage () is not triggered​​ I have a static site hosted on S3 with CloudFront distro on top of it....
Read more >
HTML : React Native WebView onMessage doesn't do anything
HTML : React Native WebView onMessage doesn't do anything [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] HTML : React Native ......
Read more >
webview-onmessage - Expo Snack
webview ->react-native onMessage example.
Read more >
React Native WebView: A complete guide - LogRocket Blog
This package will work both on Android and iOS devices. Here is a short overview of the terminal commands: # cd into the...
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