-
Notifications
You must be signed in to change notification settings - Fork 939
Listen javascript function from flutter #523
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a6646c
add javascriptChannelMessage for android
4f7aa1f
Return JavascriptMessage object for onPostMessage
c726f15
add javascriptChannelMessage for ios
298868c
Add JavascriptChannel abstraction in dart code
ivanasen 7fb61ea
Merge pull request #1 from ivanasen/javascript-channel-dart
rinlv cc2039e
Revert "fixed ios example for xcode 10"
8aa9dea
fixed by comment https://github.com/fluttercommunity/flutter_webview_…
1f4c8fb
Merge branch 'master' into master
charafau 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
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
60 changes: 60 additions & 0 deletions
60
android/src/main/java/com/flutter_webview_plugin/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,60 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package com.flutter_webview_plugin; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.webkit.JavascriptInterface; | ||
|
||
import java.util.HashMap; | ||
|
||
import io.flutter.plugin.common.MethodChannel; | ||
|
||
/** | ||
* 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; | ||
private final Handler platformThreadHandler; | ||
|
||
/** | ||
* @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, Handler platformThreadHandler) { | ||
this.methodChannel = methodChannel; | ||
this.javaScriptChannelName = javaScriptChannelName; | ||
this.platformThreadHandler = platformThreadHandler; | ||
} | ||
|
||
// Suppressing unused warning as this is invoked from JavaScript. | ||
@SuppressWarnings("unused") | ||
@JavascriptInterface | ||
public void postMessage(final String message) { | ||
Runnable postMessageRunnable = | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
HashMap<String, String> arguments = new HashMap<>(); | ||
arguments.put("channel", javaScriptChannelName); | ||
arguments.put("message", message); | ||
methodChannel.invokeMethod("javascriptChannelMessage", arguments); | ||
} | ||
}; | ||
if (platformThreadHandler.getLooper() == Looper.myLooper()) { | ||
postMessageRunnable.run(); | ||
} else { | ||
platformThreadHandler.post(postMessageRunnable); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.