AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Tuesday 29 March 2016

How to display browser notifications from web application?

In this blog article I will discuss how to display browser notifications from the web application? You can push notifications for event reminders, message information, etc. Let's start
Step 1: Show Permission for Notification
Add below java-script code in webpage.
document.addEventListener('DOMContentLoaded', function () 
{

if (Notification.permission !== "granted")
{
Notification.requestPermission();
}

});


Notification.requestPermission() will give alert for notification permission.

Step 2: Custom Notification function

function customnotify(title,desc,url) 
{

  if (Notification.permission !== "granted")
  {
   Notification.requestPermission();
  }
  else
  {
   var notification = new Notification(title, {
   icon:'http://Your_Website.com/logo.png',
   body: desc,
   });

   /* Remove the notification from Notification Center when clicked.*/
   notification.onclick = function () {
   window.open(url); 
   };

   /* Callback function when the notification is closed. */
   notification.onclose = function () {
   console.log('Notification closed');
   };

  }
}
Now notification is setup on a web page you can call any notification from web page just calling customnotify function.

For Example:
customnotify('Shivam','Hello how are you','http://www.aspbucket.com')
Call above fuction & Notification will start display.

0 comments :

Post a Comment

  • Popular Posts
  • Comments