0

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"); } 

}

6
  • Why not using firebase for the whole app? Commented Dec 8, 2017 at 6:18
  • I am not familiar with fire base and I don't know how to use it Commented Dec 8, 2017 at 13:43
  • Okay no problem its easy to implement for your app! Although I can not answer it here because its not your question. Check me directly here in my email [email protected] I can help if you are interested. You can post and something in your site and send directly notifications to your app users. Commented Dec 8, 2017 at 20:16
  • Send me an email in [email protected] with the source code Commented Dec 8, 2017 at 20:39
  • Okay! I will send you! Commented Dec 9, 2017 at 6:29

1 Answer 1

0

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> 
Sign up to request clarification or add additional context in comments.

6 Comments

Do I need to add permission on the android manifest file and if I do what it would be ?
Only internet permissions
Happy to Help.. and if you find it useful Then upvote and accept the answer
One more question so this will send the user a push notification to his or her
Devices when a new content is added to the website ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.