【问题标题】:How to display website notifications as app notifications android studio webview如何将网站通知显示为应用程序通知android studio webview
【发布时间】:2021-02-10 09:54:20
【问题描述】:

尝试在此处搜索解决方案,但它需要注册。 firebase 中的每个用户 Website notifications in Webview Android App

此类问题的所有其他答案都以 firebase 告终,

如何实现正常的网站通知,在我们的网站上,用户也会收到个性化的通知。

有没有人尝试过任何其他解决方案? 因为它只是需要显示的正常网站通知。

【问题讨论】:

标签: java android webview


【解决方案1】:

您可以使用 javascript 与 WebView 通信以显示通知:

  1. 在android中创建JavascriptInterface:

     public class WebAppInterface {
         Context mContext;
    
         WebAppInterface(Context c) {
             mContext = c;
         }
    
         /** Receive messages from the web page */
         @JavascriptInterface
         public void showToast(String message) {
             // push notifications to android system ....
             ...
             ...
         }
     }
    
  2. 为 webview 添加 javascript 接口:

webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPush");

  1. 在网页的javascript代码中执行推送通知:

     // on message recivied from server.
     if (AndroidPush) {
         // call android webview to push notifications.
         AndroidPush.showToast('message...');
     } else {
         // call notification api of browser.
         ...
     }
     ......
    

【讨论】:

  • 我只需要在 android studio 代码中添加这些,还是需要在网站上也放一些?
  • 有没有这个的github项目示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 2018-05-29
相关资源
最近更新 更多