Skip to content

Commit 59a4c33

Browse files
added javascript interface to listen post messages
1 parent 5d5316f commit 59a4c33

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.view.View;
1111
import android.view.ViewGroup;
1212
import android.webkit.CookieManager;
13+
import android.webkit.JavascriptInterface;
1314
import android.webkit.ValueCallback;
1415
import android.webkit.WebChromeClient;
1516
import android.webkit.WebSettings;
@@ -171,6 +172,7 @@ public boolean onShowFileChooser(
171172
return true;
172173
}
173174
});
175+
webView.addJavascriptInterface(new WebAppInterface(), "Android");
174176
}
175177

176178
private void clearCookies() {
@@ -339,4 +341,13 @@ void stopLoading(MethodCall call, MethodChannel.Result result){
339341
webView.stopLoading();
340342
}
341343
}
344+
345+
public class WebAppInterface {
346+
@JavascriptInterface
347+
public void getPostMessage(String value){
348+
Map<String, Object> orderMap = new HashMap<>();
349+
orderMap.put("order", value);
350+
FlutterWebviewPlugin.channel.invokeMethod("onOrderRequest", orderMap);
351+
}
352+
}
342353
}

lib/src/base.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class FlutterWebviewPlugin {
2929
final _onScrollXChanged = StreamController<double>.broadcast();
3030
final _onScrollYChanged = StreamController<double>.broadcast();
3131
final _onHttpError = StreamController<WebViewHttpError>.broadcast();
32+
final _lightningLinkStream = StreamController<String>.broadcast();
3233

3334
Future<Null> _handleMessages(MethodCall call) async {
3435
switch (call.method) {
@@ -54,6 +55,9 @@ class FlutterWebviewPlugin {
5455
case 'onHttpError':
5556
_onHttpError.add(WebViewHttpError(call.arguments['code'], call.arguments['url']));
5657
break;
58+
case 'onOrderRequest':
59+
_lightningLinkStream.add(call.arguments['order']);
60+
break;
5761
}
5862
}
5963

@@ -76,6 +80,8 @@ class FlutterWebviewPlugin {
7680

7781
Stream<WebViewHttpError> get onHttpError => _onHttpError.stream;
7882

83+
Stream<String> get lightningLinkStream => _lightningLinkStream.stream;
84+
7985
/// Start the Webview with [url]
8086
/// - [headers] specify additional HTTP headers
8187
/// - [withJavascript] enable Javascript or not for the Webview
@@ -188,6 +194,7 @@ class FlutterWebviewPlugin {
188194
_onScrollXChanged.close();
189195
_onScrollYChanged.close();
190196
_onHttpError.close();
197+
_lightningLinkStream.close();
191198
_instance = null;
192199
}
193200

0 commit comments

Comments
 (0)