Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with Android 10 and below WebViewFeature.WEB_MESSAGE_LISTENER is false, receive postMessage message #2474

Open
wants to merge 1 commit into
base: v6.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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