Make a desktop notification using JavaScript is simple

This part of code is responsible for desktop notification 🙂


function notifyMe(title, options) {
// Check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}

// Check whether notification permissions have alredy been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification(title, options);
}

// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification(title, options);
}
});
}
}
var title = "ddeveloperr's Notification";
var options = {
body: "ddeveloperrs Notification Body",
icon: "http://i.imgur.com/7RdS6AH.png"
}
notifyMe(title, options);

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s