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.

window.ReactNativeWebView.postMessage(data) and onMessage doesn't work on release apk mode. [only works in debug mode]

See original GitHub issue

Bug description:

Hello, i’m trying to send data from my web page to my react native app through this library, using a phone phisical android and it only perfectly works in debug mode. When i build it for production, it doesn’t work, window.ReactNativeWebView.postMessage(data) does nothing.

Expected behavior: Capure the data sent from my web page in release apk mode

Screenshots/Videos: Code from my web page Screenshot at 2021-12-19 14-40-24

code from my RN app Screenshot at 2021-12-19 14-56-43

Environment:

  • OS: android
  • OS version: 8.1.1
  • react-native version: 0.63.4
  • react-native-webview version: 11.15.0 => I was trying another version 9.0.2 and it didn’t work either

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
isaac-tcscommented, Feb 15, 2022

I’m having same issue. In my case, I’m trying to get events from iframe video but it only works on browser and not on react native via webview.

    window.onmessage = function (event) {
      if (event.data.event == "play") {
        alert('lll');                                     // this won't fire
        sendEvent(event.data.event, event.data.data);
      }
      if (event.data.event == "pause") {
        sendEvent(event.data.event, event.data.data);
      }
      if (event.data.event == "ready") {
        sendEvent(event.data.event, event.data.data);
      }
      if (event.data.event == "timeupdate") {
        sendEvent('playing', event.data.data);
      }
      if (event.data.event == "ended") {
        sendEvent('end', event.data.data);
      }
    };

    const sendEvent = (evt, data) => {
      // Passes events through the bridge
      console.log("event", evt);
      var payload = {
        name: evt,
        data: data,
      };
      window.ReactNativeWebView.postMessage(JSON.stringify(payload));
    };
  const INJECTEDJAVASCRIPT =
    "const meta = document.createElement('meta'); meta.setAttribute('content', 'width=device-width, initial-scale=1, maximum-scale=0.99, user-scalable=0'); meta.setAttribute('name', 'viewport'); document.getElementsByTagName('head')[0].appendChild(meta);";

<WebView
      allowsFullscreenVideo={true}
      onLoadEnd={() => console.log('live load enddddddddddddddddd')}
      source={{
        html: LivePlayer(videoId),
      }}
      javaScriptEnabled={true}
      ref={ref}
      onMessage={onBridgeMessage}
      bounces={false}
      scrollEnabled={false}
      scalesPageToFit={scalesPageToFit}
      onError={error => console.error(error)}
      style={[
        {
          marginTop: -8,
          marginLeft: -10,
        },
        style,
      ]}
      containerStyle={containerStyle}
      setBuiltInZoomControls={false}
      setDisplayZoomControls={false}
      automaticallyAdjustContentInsets
      onNavigationStateChange={a => console.log(a?.url)}
      injectedJavaScript={INJECTEDJAVASCRIPT}
    />
0reactions
github-actions[bot]commented, May 23, 2022

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native WebView postMessage does not work
I had the same problem and fixed it by adding the event listener to the document instead of the window. Change: window.
Read more >
App doesn't work in release apk but works fine in debug mode
My app works fine in debug mode but when i make its release build blank screen comes up as soon as internet is...
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 html postMessage can not reach to WebView ...
Coding example for the question react native html postMessage can not reach to WebView-React Native.
Read more >
postmessage webview react native Code Example
if(navigator.appVersion.includes('Android')){ document.addEventListener("message", function (data) { alert("you are in android OS"); } ...
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