Saturday, April 16, 2016

Browser Notification

In this tutorial, I am showing how to create own browser random time notification.

First we need to craete javascript / jquery file:

/*Dharambir Singh*/

var articles = [
["Make a healthy choice"],
["We’re in the business of free education"],
["Something unique and worth a try"]
];


setTimeout(function(){ 
var x = Math.floor((Math.random() * 3) + 1);
var title=articles[x][0];
var desc='dharamart.blogspot.com';
var url=articles[x][1];
notifyBrowser(title,desc,url);
}, 200000);



document.addEventListener('DOMContentLoaded', function () 
{
    
if (Notification.permission !== "granted")
{
Notification.requestPermission();
}

$( document ).ready(function() {
var x = Math.floor((Math.random() * 10) + 1);
var title=articles[x][0];
var desc='www.dharamart.blogspot.com.';
var url=articles[x][1];
notifyBrowser(title,desc,url);
e.preventDefault();
});
});
function notifyBrowser(title,desc,url) 
{
if (!Notification) {
console.log('Desktop notifications not available in your browser..'); 
return;
}
if (Notification.permission !== "granted")
{
Notification.requestPermission();
}
else {
var notification = new Notification(title, {
icon:'https://www.dharamart.blogspot/images/logo.png',
body: desc,
});
notification.onclick = function () {
window.open("http://www.dharamart.blogspot.com");      
};
notification.onclose = function () {
console.log('Notification closed');
};

}
}


please replace dharamart.blogspot.com with your respected domain name.

and just attache this js file to your index.page or in any page where you exactly wwant to place this notification.


No comments:

Post a Comment

Dharamart.blogspot.in