Android WebView推送通知?

DRO*_*mes 8 html javascript android android-webview

我需要通过android webview 发送通知(不一定是推送通知).我看到它Notification APIMDN上的Android Webview不兼容.我看到的其他API似乎是基于window.notification.

有没有人知道通过android webview发送通知的任何API或JS?

我从6个月前看过这篇文章,基本没有任何活动,只是模糊地提到了火柱.这会有帮助吗?

谢谢你的回答!

Bla*_*ack 5

阅读将 JavaScript 代码绑定到 Android 代码的文档条目

\n\n

这允许您使用javascript来触发android代码的执行。

\n\n

首先你必须在android上注册Javascript接口,这样你就可以从javascript触发android代码。

\n\n

爪哇

\n\n
WebView webView = (WebView) findViewById(R.id.webview);\nwebView.addJavascriptInterface(new WebAppInterface(this), "Android");\n
Run Code Online (Sandbox Code Playgroud)\n\n

并定义一个方法,该方法在调用 javascript 时执行您的操作。在这个例子中展示了一个吐司。

\n\n
public class WebAppInterface {\n    Context mContext;\n\n    /** Instantiate the interface and set the context */\n    WebAppInterface(Context c) {\n        mContext = c;\n    }\n\n    /** Show a toast from the web page */\n    @JavascriptInterface\n    public void showToast(String toast) {\n        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x86\x91 您需要更改该部分以显示推送通知。\xe2\x86\x91

\n\n

然后你可以像这样从 javascript 触发 android 代码:

\n\n
<input type="button" value="Say hello" onClick="showAndroidToast(\'Hello Android!\')" />\n\n<script type="text/javascript">\n    function showAndroidToast(toast) {\n        Android.showToast(toast);\n    }\n</script>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我自己没有尝试过。但我会尝试创建一个 javascript 方法,该方法以一定的时间间隔向服务器发出 ajax 请求,并检查是否有新的通知要发送,如果为 true,则调用 android 代码来显示消息。

\n\n

但是,您必须确保仅以某种方式显示一次通知...也许设置一个包含通知 ID 的 cookie 并将其设置为 true,以便 android 代码不会再次被触发。

\n\n

您需要提供通知,例如 JSON 格式的 .json 文件。您可以将该 .json 文件上传到您的网络服务器的某个位置。然后将内容传递给android并进行相应的解析。

\n