Browse Source

Implement direct communication between Android code and JS

Ivari Tölp 4 years ago
parent
commit
bb1a8b501e

+ 30
- 2
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -41,12 +41,15 @@ import com.facebook.react.views.scroll.ScrollEvent;
41 41
 import com.facebook.react.views.scroll.ScrollEventType;
42 42
 import com.facebook.react.views.scroll.OnScrollDispatchHelper;
43 43
 import com.facebook.react.bridge.Arguments;
44
+import com.facebook.react.bridge.CatalystInstance;
44 45
 import com.facebook.react.bridge.LifecycleEventListener;
45 46
 import com.facebook.react.bridge.ReactContext;
46 47
 import com.facebook.react.bridge.ReadableArray;
47 48
 import com.facebook.react.bridge.ReadableMap;
48 49
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
49 50
 import com.facebook.react.bridge.WritableMap;
51
+import com.facebook.react.bridge.WritableNativeArray;
52
+import com.facebook.react.bridge.WritableNativeMap;
50 53
 import com.facebook.react.common.MapBuilder;
51 54
 import com.facebook.react.common.build.ReactBuildConfig;
52 55
 import com.facebook.react.module.annotations.ReactModule;
@@ -1086,6 +1089,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1086 1089
     }
1087 1090
 
1088 1091
     public void onMessage(String message) {
1092
+      ReactContext reactContext = (ReactContext) this.getContext();
1093
+      RNCWebView mContext = this;
1094
+
1089 1095
       if (mRNCWebViewClient != null) {
1090 1096
         WebView webView = this;
1091 1097
         webView.post(new Runnable() {
@@ -1096,16 +1102,38 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1096 1102
             }
1097 1103
             WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
1098 1104
             data.putString("data", message);
1099
-            dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1105
+
1106
+            if (reactContext != null) {
1107
+              mContext.sendDirectMessage(reactContext, data);
1108
+            } else {
1109
+              dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1110
+            }
1100 1111
           }
1101 1112
         });
1102 1113
       } else {
1103 1114
         WritableMap eventData = Arguments.createMap();
1104 1115
         eventData.putString("data", message);
1105
-        dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1116
+
1117
+        if (reactContext != null) {
1118
+          this.sendDirectMessage(reactContext, eventData);
1119
+        } else {
1120
+          dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1121
+        }
1106 1122
       }
1107 1123
     }
1108 1124
 
1125
+    protected void sendDirectMessage(ReactContext reactContext, WritableMap data) {
1126
+      CatalystInstance catalystInstance = reactContext.getCatalystInstance();
1127
+
1128
+      WritableNativeMap event = new WritableNativeMap();
1129
+      event.putMap("nativeEvent", data);
1130
+
1131
+      WritableNativeArray params = new WritableNativeArray();
1132
+      params.pushMap(event);
1133
+
1134
+      catalystInstance.callFunction("WebViewMessageHandler", "onMessage", params);
1135
+    }
1136
+
1109 1137
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
1110 1138
       super.onScrollChanged(x, y, oldX, oldY);
1111 1139
 

+ 1
- 1
index.js View File

@@ -1,4 +1,4 @@
1
-import WebView from './lib/WebView';
1
+import WebView from './src/WebView';
2 2
 
3 3
 export { WebView };
4 4
 export default WebView;

+ 6
- 0
src/WebView.android.tsx View File

@@ -10,6 +10,8 @@ import {
10 10
   findNodeHandle,
11 11
 } from 'react-native';
12 12
 
13
+import BatchedBridge from 'react-native/Libraries/BatchedBridge/BatchedBridge';
14
+
13 15
 import invariant from 'invariant';
14 16
 
15 17
 import {
@@ -70,6 +72,10 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
70 72
 
71 73
   webViewRef = React.createRef<NativeWebViewAndroid>();
72 74
 
75
+  componentDidMount = () => {
76
+    BatchedBridge.registerCallableModule('WebViewMessageHandler', this);
77
+  }
78
+
73 79
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
74 80
 
75 81
   goForward = () => {