Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

android - Adding notification on 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");

}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In java File

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...