简体   繁体   English

Android WebView 推送通知?

[英]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!感谢您的回答!

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.

相关问题 Android Webview桌面通知支持 - Android Webview Desktop Notification Support 通过Android / iOS中的浏览器发送推送通知 - Send push notification via browser in Android/iOS 使用自定义GCM参数将通知推送到Android设备 - Push Notification to android device with custom GCM parameters 在状态栏Android Cordova中推送通知 - Push Notification in status bar Android Cordova 获取推送通知的appid-手机差距(Android) - Get appid for push Notification - phone gap (android) 如何在 webview 应用程序中发送推送通知,其中通知在网站和 PWA 上进行 - How to send Push Notification In webview app where as notification going on website and PWA Android推送通知 - 如何在单击单个推送通知的操作按钮时获取通知数据 - Android Push Notification - How to get notification data when you click on action button of individual push notification Ionic Push Android推送通知返回“未定义”消息 - Ionic Push Android push notification returning “undefined” message Android Firebase推送通知用于两个不同的数据库路径 - Android firebase push notification for two different database path 创建网络推送通知(不是用于Android应用程序,而是仅用于网站)? - create web push notification(not for android app, but for website only)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM