Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Send Desktop Notifications in Chrome (http://www.go4expert.com/articles/send-desktop-notifications-chrome-t30017/)

pradeep 25Feb2014 23:56

Send Desktop Notifications in Chrome
 
Desktop notifications for web applications is a great plus, the user can be notified irrespective of the current window/tab they are using. Chrome, which has the largest share of the browser market, supports desktop notification, also Safari, or any webkit based browser.

You can easily implement it you web applications, like mail clients (GMail has this feature), CRMs, Stock Market apps, and anything you can think may use this feature. All you need to some JavaScript and a server side script to send updates, or you can do without the server part also.

Now, let's get our hands dirty with some desktop notification code.

Implementing Desktop notifications on Webkit based browsers



Try this on Chrome/Safari, first time a popup will appear asking for permission.

Code: JavaScript

if (window.webkitNotifications) { // check if browser has support
    var has_perm = window.webkitNotifications.checkPermission(); // check if permission granted
    if (has_perm == 0) { // 0 means has permission to show desktop notification
        var notification = window.webkitNotifications.createNotification(
            'http://g4e.com/logo.png',
            'Welcome!',
            'Here is the notification'
        );
        notification.show();
    } else {
        window.webkitNotifications.requestPermission();
    }
}


Additionally, you may also control what happens when the notification is clicked. Google recently launched a Rich Desktop notification where one can control the style elements of the notification body, that we'll cover in some other article in the future.

shabbir 26Feb2014 08:52

Re: Send Desktop Notifications in Chrome
 
The same can be done for Firefox (though experimental like Chrome) as well and it need not be only for Chrome. https://developer.mozilla.org/en/doc...I/notification


All times are GMT +5.5. The time now is 03:11.