Skip to content

Commit

Permalink
added javascript interface to listen post messages
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Jan 27, 2019
1 parent 5d5316f commit 59a4c33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
Expand Down Expand Up @@ -171,6 +172,7 @@ public boolean onShowFileChooser(
return true;
}
});
webView.addJavascriptInterface(new WebAppInterface(), "Android");
}

private void clearCookies() {
Expand Down Expand Up @@ -339,4 +341,13 @@ void stopLoading(MethodCall call, MethodChannel.Result result){
webView.stopLoading();
}
}

public class WebAppInterface {
@JavascriptInterface
public void getPostMessage(String value){
Map<String, Object> orderMap = new HashMap<>();
orderMap.put("order", value);
FlutterWebviewPlugin.channel.invokeMethod("onOrderRequest", orderMap);
}
}
}
7 changes: 7 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FlutterWebviewPlugin {
final _onScrollXChanged = StreamController<double>.broadcast();
final _onScrollYChanged = StreamController<double>.broadcast();
final _onHttpError = StreamController<WebViewHttpError>.broadcast();
final _lightningLinkStream = StreamController<String>.broadcast();

Future<Null> _handleMessages(MethodCall call) async {
switch (call.method) {
Expand All @@ -54,6 +55,9 @@ class FlutterWebviewPlugin {
case 'onHttpError':
_onHttpError.add(WebViewHttpError(call.arguments['code'], call.arguments['url']));
break;
case 'onOrderRequest':
_lightningLinkStream.add(call.arguments['order']);
break;
}
}

Expand All @@ -76,6 +80,8 @@ class FlutterWebviewPlugin {

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

Stream<String> get lightningLinkStream => _lightningLinkStream.stream;

/// Start the Webview with [url]
/// - [headers] specify additional HTTP headers
/// - [withJavascript] enable Javascript or not for the Webview
Expand Down Expand Up @@ -188,6 +194,7 @@ class FlutterWebviewPlugin {
_onScrollXChanged.close();
_onScrollYChanged.close();
_onHttpError.close();
_lightningLinkStream.close();
_instance = null;
}

Expand Down

0 comments on commit 59a4c33

Please sign in to comment.