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.

postMessage or onMessage not working android?

See original GitHub issue

I need to send a message to react native through the website. It works as expected in ios when I do it as below. But it doesn’t work on android

website code (x.com)

let sendMessageToNative = () =>{ 
        let url = window.location.href
        window.ReactNativeWebView && window.ReactNativeWebView.postMessage(url, '*')
    }
   sendMessageToNative() 

react native code

<WebView

        allowsBackForwardNavigationGestures={true}
        originWhitelist={['*']}
        source={{ uri: 'x.com' }}
        startInLoadingState={true}
        javaScriptEnabledAndroid={true}
        javaScriptEnabled={true}
        onMessage={event => {
            console.log("eee", event.nativeEvent.data)
            alert('MESSAGE >>>>' + event.nativeEvent.data)
        }}

        ref={webviewRef}

        />

Expected behavior: The url I sent on ios appears on the screen as an alert. This does not happen on Android. The same I sent in the string.the same must be on android

Environment:

  • OS: Android
  • OS version: Android Version 11 emilator or xiaomi mi 8 lite android version 10
  • react-native version: 38
  • react-native-webview version: 9.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
niezichengcommented, Nov 22, 2021

After trying all the above, I still have this problem: it does not work normally on ios but not on android

0reactions
giacomoalonzicommented, Aug 5, 2022

still searching for solutions

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native WebView postMessage does not work
Does anyone know why the web app is not receiving the messages? Tested with both Android and iOS. Related documentation: https://facebook.github ...
Read more >
MessageEvent - Web APIs - MDN Web Docs - Mozilla
The MessageEvent interface represents a message received by a target object. This is used to represent messages in:.
Read more >
Fixing React Native WebView's postMessage for iOS
Our original problem is already solved — onMessage works with this WebView. However, since we have overwritten the internal onNavigationStateChange listener, ...
Read more >
postMessage/onMessage no longer working | Velo by Wix
I am facing similar issue and did not understand the solution completely. Is it possible for you to share the sample code on...
Read more >
chrome.runtime - Chrome Developers
onMessage. event. This event is fired when postMessage is called by the other end of the port. The onMessage.addListener function looks like: (callback: ......
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