我无法让React Native WebView postMessage正常工作.React本机应用程序成功接收来自web应用程序的post message sendt.但web应用程序不会接收来自React本机应用程序的消息.

My simple web app

<html>
<body>

<button>Send post message from web</button>
<div>Post message log</div>
<textarea style="height: 50%; width: 100%;" readonly></textarea>

<script>
var log = document.querySelector("textarea");

document.querySelector("button").onclick = function() {
    console.log("Send post message");

    logMessage("Sending post message from web..");
    window.postMessage("Post message from web", "*");
}

window.addEventListener("message", function(event) {
    console.log("Received post message", event);

    logMessage(event.data);
}, false);

function logMessage(message) {
    log.append((new Date()) + " " + message + "\n");
}

</script>

</body>
</html>

此web应用的托管时间为:https://popping-heat-6062.firebaseapp.com/

My simple React Native app

import React, {Component} from "react";
import {AppRegistry, Text, View, TouchableHighlight, WebView} from "react-native";

export default class WevViewApp extends Component {

    constructor( props ) {
        super( props );

        this.webView = null;
    }

    onMessage( event ) {
        console.log( "On Message", event.nativeEvent.data );
    }

    sendPostMessage() {
        console.log( "Sending post message" );
        this.webView.postMessage( "Post message from react native" );
    }

    render() {
        return (
            <View style={{flex: 1}}>
                <TouchableHighlight style={{padding: 10, backgroundColor: 'blue', marginTop: 20}} onPress={() => this.sendPostMessage()}>
                    <Text style={{color: 'white'}}>Send post message from react native</Text>
                </TouchableHighlight>
                <WebView
                    style={{flex: 1}}
                    source={{uri: 'https://popping-heat-6062.firebaseapp.com'}}
                    ref={( webView ) => this.webView = webView}
                    onMessage={this.onMessage}
                />
            </View>
        );
    }
}

AppRegistry.registerComponent( 'WevViewApp', () => WevViewApp );

Expected result

  1. 点击"从网络发送帖子"按钮发送
  2. 点击"从React Native发送帖子"按钮

Actual result

  1. Web app successfully向React Native app发送消息,并记录在调试器窗口中
  2. React Native app unsuccessfully向web app发送消息,但不会在web文本区域打印出来

有人知道为什么web应用程序没有收到这些消息吗?

在Android和iOS上都进行了测试.

相关文件:https://facebook.github.io/react-native/docs/webview.html


Edit:经历了一种可能指出问题的行为.

当直接在web浏览器中测试"从web发送邮件"按钮时,textarea会记录它已发送的邮件以及收到的自己的邮件.

Thu Dec 15 2016 10:20:34 GMT+0100 (CET) Sending post message from web..
Thu Dec 15 2016 10:20:34 GMT+0100 (CET) Post message from web

当从React原生应用程序在WebView中try 相同操作时,textarea只会打印出来

Thu Dec 15 2016 10:20:34 GMT+0100 (CET) Sending post message from web..

网络视图会劫持窗口吗.postMessage方法并注入自定义方法?

这在documentation篇文章中提到:

设置此属性会将postMessage global注入到

也许这是错误的,这就是问题所在?

推荐答案

我也遇到了同样的问题,并通过将事件侦听器添加到文档而不是窗口来修复它.改变:

window.addEventListener("message", ...

致:

document.addEventListener("message", ...

React-native相关问答推荐

任务:react-native-async-storage_async-storage:generateDebugRFile FAILED

用图像 react native 动画进度条

无法在 M1 zsh 上运行 adb segmentation fault adb

需要以前的props和state回调的静态 getDerivedStateFromProps?

无法在 React Native iOS 模拟器中按 URI 显示图像

react-native redux 和 ListView

打包程序无法启动

如何将抖动动画添加到视图(react-native)?

命令失败:gradlew.bat installDebug error whenever installing dependencies like navigation, firebase, icons etc in React-Native

如何在react-native 中加密和解密文本?

React-Native + crypto:如何在 React-Native 中生成 HMAC?

在 React Native 中使用多个上下文提供程序的更好方法

没有找到 `React-fishhook` 的 podspec

我可以在 react-native 中使用 reactJS 库吗?

React Native TextInput blur TouchableHighlight 按下事件

在 OPTIONS 响应后使 fetch API 与 CORS 一起工作

如何在 React Native 的 MapView 中设置标记

试图在一个本应是不可变且已被冻结的对象上设置密钥

React 本机矢量图标在当前版本 0.60 上不起作用

如何在 react-native 中为带有图标的按钮建模