Browse Source

fix(android): onMessage on android to return baseEvent(#755)

SiDevesh 5 years ago
parent
commit
282f81dcdd

+ 15
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -915,7 +915,21 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
915 915
     }
916 916
 
917 917
     public void onMessage(String message) {
918
-      dispatchEvent(this, new TopMessageEvent(this.getId(), message));
918
+      if (mRNCWebViewClient != null) {
919
+        WebView webView = this;
920
+        webView.post(new Runnable() {
921
+          @Override
922
+          public void run() {
923
+            WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
924
+            data.putString("data", message);
925
+            dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
926
+          }
927
+        });
928
+      } else {
929
+        WritableMap eventData = Arguments.createMap();
930
+        eventData.putString("data", message);
931
+        dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
932
+      }
919 933
     }
920 934
 
921 935
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {

+ 3
- 5
android/src/main/java/com/reactnativecommunity/webview/events/TopMessageEvent.kt View File

@@ -1,13 +1,13 @@
1 1
 package com.reactnativecommunity.webview.events
2 2
 
3
-import com.facebook.react.bridge.Arguments
3
+import com.facebook.react.bridge.WritableMap
4 4
 import com.facebook.react.uimanager.events.Event
5 5
 import com.facebook.react.uimanager.events.RCTEventEmitter
6 6
 
7 7
 /**
8 8
  * Event emitted when there is an error in loading.
9 9
  */
10
-class TopMessageEvent(viewId: Int, private val mData: String) : Event<TopMessageEvent>(viewId) {
10
+class TopMessageEvent(viewId: Int, private val mEventData: WritableMap) : Event<TopMessageEvent>(viewId) {
11 11
   companion object {
12 12
     const val EVENT_NAME = "topMessage"
13 13
   }
@@ -19,8 +19,6 @@ class TopMessageEvent(viewId: Int, private val mData: String) : Event<TopMessage
19 19
   override fun getCoalescingKey(): Short = 0
20 20
 
21 21
   override fun dispatch(rctEventEmitter: RCTEventEmitter) {
22
-    val data = Arguments.createMap()
23
-    data.putString("data", mData)
24
-    rctEventEmitter.receiveEvent(viewTag, EVENT_NAME, data)
22
+    rctEventEmitter.receiveEvent(viewTag, EVENT_NAME, mEventData)
25 23
   }
26 24
 }