When I run my application through visual studio code (it doesn't matter if connected to an emulator or a real device), there is no problem. After I release my project, I don't get any response from onMessage when I install my application on my real device with apk. For example, when I run apk on real device, I can't see alert('3'). Also other codes (usestate, etc...) inside onMessage doesn't work, neither. Only 1 and 2 appear.
import {WebView} from 'react-native-webview';
import React, {useEffect, useRef, useState} from 'react';
...
const webView = useRef();
...
const getDslBootTime = async () => {
updateLogs('1');
await delay(3500);
webView.current.injectJavaScript(`location.reload();`);
//DSL Boot Time
webView.current.injectJavaScript(`window.ReactNativeWebView.postMessage(
JSON.stringify({
"name": "DSL Boot Tıme",
"stat" :
document.querySelectorAll('[id="boot_days"]')[0].innerHTML + " Day " +
document.querySelectorAll('[id="boot_hours"]')[0].innerHTML + " Hour " +
document.querySelectorAll('[id="boot_minutes"]')[0].innerHTML + " Mınute " +
document.querySelectorAll('[id="boot_seconds"]')[0].innerHTML + " Second"
})
)
`);
updateLogs('2');
};
const updateLogs = log => {
alert(log);
};
...
<WebView
ref={webView}
source={{
uri: url,
}}
onMessage={event => {
updateLogs('3');
const js_data = JSON.parse(event.nativeEvent.data);
...
}}
/>
...