Skip to content

Commit

Permalink
fix: 兼容android10及以下不支持addWebMessageListener
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshaocong committed Dec 23, 2024
1 parent 751a104 commit 52ab40c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion flutter_inappwebview/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ dependencies:
flutter:
sdk: flutter
flutter_inappwebview_platform_interface: ^1.0.10
flutter_inappwebview_android: ^1.0.12
flutter_inappwebview_android:
path: ../flutter_inappwebview_android
flutter_inappwebview_ios: ^1.0.13
flutter_inappwebview_macos: ^1.0.11
flutter_inappwebview_web: ^1.0.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,12 @@ public void onReceiveValue(Boolean value) {
if (webView != null) {
Map<String, Object> webMessageListenerMap = (Map<String, Object>) call.argument("webMessageListener");
WebMessageListener webMessageListener = WebMessageListener.fromMap(webView, webView.getPlugin().messenger, webMessageListenerMap);
if (webView instanceof InAppWebView && WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
try {
webView.addWebMessageListener(webMessageListener);
result.success(true);
} catch (Exception e) {
result.error(LOG_TAG, e.getMessage(), null);
}
} else {
result.success(true);
}
} else {
result.success(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,8 @@ public void addWebMessageListener(@NonNull WebMessageListener webMessageListener
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
WebViewCompat.addWebMessageListener(this, webMessageListener.jsObjectName, webMessageListener.allowedOriginRules, webMessageListener.listener);
webMessageListeners.add(webMessageListener);
} else {
addJavascriptInterface(webMessageListener, webMessageListener.jsObjectName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.pichillilorenzo.flutter_inappwebview_android.webview.web_message;

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import androidx.annotation.NonNull;
Expand All @@ -21,6 +24,7 @@
import com.pichillilorenzo.flutter_inappwebview_android.webview.in_app_webview.InAppWebView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -171,6 +175,23 @@ public void assertOriginRulesValid() throws Exception {
}
}

// Suppressing unused warning as this is invoked from JavaScript.
@SuppressWarnings("unused")
@JavascriptInterface
public void postMessage(@NonNull final String message) {
System.out.println("postMessage " + message);
Map<String, Object> map = new HashMap<>();
map.put("data", message);
map.put("type", WebMessageCompat.TYPE_STRING);
WebMessageCompatExt webMessage = WebMessageCompatExt.fromMap(map);
if (channelDelegate != null) {
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(() -> channelDelegate.onPostMessage(webMessage,
null,
false));
}
}

public void postMessageForInAppWebView(WebMessageCompatExt message, @NonNull MethodChannel.Result result) {
if (replyProxy != null && WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
Object data = message.getData();
Expand Down

0 comments on commit 52ab40c

Please sign in to comment.