This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
WebView JavasScript channels Android implementation. #1130
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ew_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/JavaScriptChannel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import android.webkit.JavascriptInterface; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * Added as a JavaScript interface to the WebView for any JavaScript channel that the Dart code sets | ||
| * up. | ||
| * | ||
| * <p>Exposes a single method named `postMessage` to JavaScript, which sends a message over a method | ||
| * channel to the Dart code. | ||
| */ | ||
| class JavaScriptChannel { | ||
| private final MethodChannel methodChannel; | ||
| private final String javaScriptChannelName; | ||
|
|
||
| /** | ||
| * @param methodChannel the Flutter WebView method channel to which JS messages are sent | ||
| * @param javaScriptChannelName the name of the JavaScript channel, this is sent over the method | ||
| * channel with each message to let the Dart code know which JavaScript channel the message | ||
| * was sent through | ||
| */ | ||
| JavaScriptChannel(MethodChannel methodChannel, String javaScriptChannelName) { | ||
| this.methodChannel = methodChannel; | ||
| this.javaScriptChannelName = javaScriptChannelName; | ||
| } | ||
|
|
||
| // Suppressing unused warning as this is invoked from JavaScript. | ||
| @SuppressWarnings("unused") | ||
| @JavascriptInterface | ||
| public void postMessage(String message) { | ||
| HashMap<String, String> arguments = new HashMap<>(); | ||
| arguments.put("channel", javaScriptChannelName); | ||
| arguments.put("message", message); | ||
| methodChannel.invokeMethod("javascriptChannelMessage", arguments); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you also add docs explaining these params? I think it's just tricky enough that it's worth saying what they do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done