Javascript Desktop Notifications

I'm not gonna write massive paragraphs about his one, but simply provide a nice example of how to implement desktop notifications for browsers running webkit in JavaScript. Who knows maybe Internet Explorer will support it some time soon?

Here's my useful function that should let you easily post notifications to the user, it includes an onclick handler and auto focuses the window on click too.

var Site = {};
Site.Notifications = {
    GainAccess: function (callback) {
        if (Notification.permission !== "granted") { 
            Notification.requestPermission(callback);
        }
    },
    Notify: function (title, message, imageAddress, onclick) {
        var notification = {};

        // Let's check if the browser supports notifications
        if (!("Notification" in window)) {
            //alert("This browser does not support system notifications");
            return;
        }

        // Let's check whether notification permissions have already been granted
        else if (Notification.permission === "granted") {
        // If it's okay let's create a notification
            notification = new Notification(title,
            {
                icon: imageAddress,
                body: message,
            });
        }

        // 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") {
                    notification = new Notification(title,
                    {
                        icon: imageAddress,
                        body: message,
                    });
                }
            });
        }

        // Add callback when notification is clicked.
        notification.onclick = function () {
            // Show user the screen.
            window.focus();

            // Close notification.
            notification.close();

            // Perform requested action.
            if (onclick != undefined && onclick != null) {
                onclick();
            }
        };
    }
}

Then you simply call it using the code below:

Site.Notifications.Notify("Hello world", "How you doin?", "http://linktomyimages.com/image.png", function () { alert ("You clicked my notification!") });

That's it.


Published at

Tags: JavaScript

Luke Alderton

Comments

Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By Helenpaups on MVC forms in Umbraco
23 May 2018, 16:48 PM
By Helenpaups on MVC forms in Umbraco
23 May 2018, 13:46 PM
By AkikoTax on MVC forms in Umbraco
20 May 2018, 06:41 AM
By Klaus on MVC forms in Umbraco
17 May 2018, 10:47 AM
By Archie on MVC forms in Umbraco
16 May 2018, 18:52 PM
By w on JQuery Unobtrusive Validation for dynamically created elements
11 May 2018, 02:00 AM
By Luke on Umbraco using Global.asax to handle events before the Request Handler.
27 Apr 2018, 18:28 PM
By Janine Gardiola on Umbraco using Global.asax to handle events before the Request Handler.
26 Apr 2018, 11:18 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development