29
2012
Creating Non-disruptive Notifications with HTML5

The HTML5 Web Notifications API (now available on Chrome) allows you to display the Growl-like notification windows outside of the web browser window. Unlike the alert dialog, the notification windows do not disrupt a user’s action, or requires extra user interactions.
Using the API is quite simple, so I tried to replicate the infamous webOS notification system in HTML5. (In case you are not familiar with webOS UI and UX, see it on YouTube).
Live demo: http://girliemac.github.com/html5-notifications-webOS-style
Basic Notifications
First, check if the browser supports the API, by looking for the Notifications property.
if (window.webkitNotifications)
Apparently, it is only supported by WebKit-based browser (well, Chrome, so far) so you need to add the vender prefix, instead of just webkitNotifications.
Next, your script needs to requests the user agent to ask a user for permission to show notifications. If you are already familiar with the geolocation API, you have seen the “Info bar” on top of the browser.

To request the permission by requestPermission(), you must invoke some event from a user, such as mouse click. See the example below:
document.getElementById('#someButton').addEventListener('click', function() {
// check if a permission is set allowed
if (window.webkitNotifications.checkPermission() == 0) { // Allowed
// do show the notifications
} else {
// request a user permission
window.webkitNotifications.requestPermission();
}
}, false);
There are two ways to create the notifications – plain text or html.
1. Plain text- use createNotification function that takes three optional params, icon image, title, and text:
var notification = window.Notifications.createNotification(
'avatar.png',
'New tweet from @girlie_mac',
'OMG, a glass of water! http://instagr.am/p/...'
);
2. HTML – use createHTMLNotification to include an external html file:
var notification = window.webkitNotifications.createHTMLNotification('tweet.html');
To show the notification:
notification.show();
Also, you can use the cancel method to close the notifications if you wish. This example below let the notification close by itself 5 second after it is displayed:
notification.ondisplay = function(event) {
setTimeout(function() {
event.currentTarget.cancel();
}, 5000);
}
Besides the ondisplay, other event handlers available are:
onclick, onshow, onerror, and onclose.
In my demo, I used the HTML notifications because only the HTML notification style allows you to fully customize the UI with CSS and add some UX with JavaScript.
To create the webOS-esque “slide-to-dismiss” effect, I used jQuery UI’s draggable and droppable. (I attempted to use the HTML5 drag and Drop, but for some reasons, I could not successfully make my code work within the notification window, although the code does work in a regular window.)
The source code is on github.
Hopefully, this API will be available on other browsers. (And Klout will replace the modal dialogs from hell to something more subtle like the web notifications in future. Can you believe once they had me to close seven modals after logging in? This has to be stopped no matter what.)
References
5 Comments to “Creating Non-disruptive Notifications with HTML5”
Leave a comment
Recent Posts
- Touchy-Feely with DOM Events: Rethinking Cross-Device User Interaction
- My Mobile HTML5 Talks Slides
- How to Enable WP8 Emulator on Mac
- HTML5 Form Validation のカスタマイズ
- HTML5 Dev Conf Slides
- HTML5 File API & XHR2 with Node.js Express
- Because I Want Mobile Web to Be A Better Platform
- Resolution in Media Queries
- HTML5: The Mobile Approach
- Creating Non-disruptive Notifications with HTML5
- Making Chupa-Chups using CSS3 Pseudo-elements
- Quick Fun: CSS3 Filter Effects
- The Day I seized The InterWeb – HTTP Status Cats
- HTML5 Form Field Validation with CSS3
- HTML5 Input Event Handlers and User-Experience
- Creating Usable Enyo UI – Buttons and Interactive Dialogs
- Thank you, Steve!
- Five CSS tricks used in Enyo JS Framework, and you can try them too!
- Open Web Camp III
- Quick Demo: CSS3 Fancy Avatar








Hmmm… I’ve heard that Notifications become available in Chrome since 2011.
Is it true?
Yarix, I think so! This is not a new news.
probably it was available even in 2010-ish in Chromium, using their older specifications.
Can the recipients receive the notification while they are not on my web page?
Burin, yes, as long as your page is not closed (e.g. stays in a tab)!
Wouldn’t that third notification kill someone? sounds dangerous… anyway, this will be a sweet new API, previously did the same thing in Java swing and it was just. plain.. unnecessary.