在Webview上添加通知 [英] Adding notification on webview

查看:63
本文介绍了在Webview上添加通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何向其中添加通知,或者是否要触发某个单词来作为通知,将不胜感激

I want to know how to add notification to this,or if I want to trigger a certain word to pop up as a notification that would be greatly appreciated

 public class NewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);
    String url = "https://www.example.com";
    WebView web = (WebView) findViewById(R.id.webView4);
    web.loadUrl(url);

    final WebView mWebView = (WebView) findViewById(R.id.webView4);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mWebView.getSettings().setSavePassword(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.getSettings().setSupportZoom(false);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setSupportMultipleWindows(false);
    mWebView.getSettings().setLightTouchEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    mWebView.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading (WebView view, String url){

            return true;
        }
    });
    mWebView.loadUrl("https://www.example.com");

}

}

推荐答案

在Java文件中

public class NewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);
    String url = "https://www.example.com";
    WebView web = (WebView) findViewById(R.id.webView4);
    web.loadUrl(url);

    final WebView mWebView = (WebView) findViewById(R.id.webView4);
    mWebView.getSettings().setJavaScriptEnabled(true);

    // Now I create an instance of my JavaScriptInterface class in my OnCreate method. 
    final JavaScriptInterface myJavaScriptInterface
        = new JavaScriptInterface(this);        
    //After the initialization of the JavaScriptInterface class, I added one more line in my OnCreate method:       
    mWebView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mWebView.getSettings().setSavePassword(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.getSettings().setSupportZoom(false);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setSupportMultipleWindows(false);
    mWebView.getSettings().setLightTouchEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    mWebView.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading (WebView view, String url){

            return true;
        }
    });
    mWebView.loadUrl("https://www.example.com");

}

public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }

        public void showNotification(String webMessage){            
             String tittle="Sample Notification";
            String subject="Sample Subject";
            String body=webMessage;

            NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify=new Notification.Builder
               (getApplicationContext()).setContentTitle(tittle).setContentText(body).
               setContentTitle(subject).setSmallIcon(R.drawable.abc).build();

               notify.flags |= Notification.FLAG_AUTO_CANCEL;
               notif.notify(0, notify);
        }
    }
}

在您的网页中,您应该编写此代码.

And in You webpage, you should write this.

<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml" debug="true">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" 

          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="viewport" content="target-densitydpi=device-dpi" />
        <script type="text/javascript">
           function init()
           {
               var testVal = document.getElementById('mytextId').value;
               AndroidFunction.showNotification(testVal);
           }
        </script>
    </head>
    <body>        
        <div style="float: left;width: 50%;">
           <input type="text" style="width: 180px;" 

                   name="myText" id="mytextId" />

        </div>
        <div style="clear: both;height: 3px;"> </div>
        <div>
          <input value="submit" type="button" name="submit" 

            id="btnSubmit" onclick="javascript:return init();" /> 
        </div>  
    </body>
</html>

这篇关于在Webview上添加通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆