[英]Android WebView push notification?
I need to send a notification (not necessarily a push notification) through an android webview.我需要通过 android webview 发送通知(不一定是推送通知)。 I saw that the
Notification API
was not compatible with Android Webview on MDN .我在MDN上看到
Notification API
与 Android Webview 不兼容。 The other APIs I saw seemed to be based off of window.notification
.我看到的其他 API 似乎是基于
window.notification
的。
Does anyone know of any API or JS that sends a notification through an android webview?有谁知道通过 android webview 发送通知的任何 API 或 JS?
I saw this post from 6 months ago with essentially no activity except a vague mention of firebase.我在 6 个月前看到这篇文章,除了模糊地提到 firebase 之外,基本上没有任何活动。 Would that be helpful?
那会有帮助吗?
Thanks for your answers!感谢您的回答!
I don't have enough "reputation" to post a comment but could that be useful to you?我没有足够的“声誉”来发表评论,但这对你有用吗?
Android Push Notification with WebView? 带有 WebView 的 Android 推送通知?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913 https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
Hopefuly you can make it work from one of those example希望您可以通过其中一个示例使其工作
Read the documentation entry for Binding JavaScript code to Android code .阅读将JavaScript 代码绑定到 Android 代码的文档条目。
This allows you to use javascript to trigger the execution of android code.这允许您使用 javascript 来触发 android 代码的执行。
First you have to register the Javascript Interface on android, so that you can trigger android code from javascript.首先,您必须在 android 上注册Javascript 接口,以便您可以从 javascript 触发 android 代码。
JAVA JAVA
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And define a method which does your action if the javascript is called.并定义一个方法,如果调用 javascript,它会执行您的操作。 In this example show a toast.
在这个例子中显示祝酒词。
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
↑ You need to change that part to show your push notification instead. ↑ 您需要更改该部分以显示您的推送通知。 ↑
↑
Then you can trigger the android code from javascript like this:然后你可以像这样从javascript触发android代码:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
I did not tried it myself.我自己没有尝试过。 But I would try to create a javascript method which makes an ajax request to a server in certain intervals and checks if there are new notifications to send and if true then call the android code to show the message.
但我会尝试创建一个 javascript 方法,它会在一定的时间间隔内向服务器发出 ajax 请求,并检查是否有新的通知要发送,如果为真,则调用 android 代码来显示消息。
However, you will have to make sure to only show the notification once somehow... maybe set a cookie containing the notification ID and set it to true, so that the android code is not getting triggered again.但是,您必须确保只以某种方式显示一次通知......也许设置一个包含通知 ID 的 cookie 并将其设置为 true,这样 android 代码就不会再次被触发。
You would need to provide the notifications for example as a.json file in JSON format.您需要提供通知,例如 JSON 格式的 .json 文件。 You can upload that.json file to your webserver somewhere.
你可以将那个.json 文件上传到你的网络服务器的某个地方。 Then pass the content to android and parse it accordingly.
然后将内容传递给android并进行相应的解析。
One Simple workaround use JSInterafce - communicate webview to native.一种简单的解决方法是使用JSInterafce - 将 webview 与本机通信。 tutorial
教程
In that JSInterface pass, the desired parameter for notification and then use android system notification API to generate the notification.在该 JSInterface 传递中,通知所需的参数然后使用 android 系统通知 API 生成通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.