Web Notifications API

By  on  

Every UI framework has the same set of widgets which have become almost essential to modern sites: modals, tooltips, button varieties, and notifications.  One problem I find is each site having their own widget colors, styles, and more -- users don't get a consistent experience.  Apparently the W3C felt the same way because they've created a Web Notifications API.  Here's how to use it!

The JavaScript

The first step is basic feature detection, as you would expect, and then you jump into the notification creation code:

if(window.Notification && Notification.permission !== "denied") {
	Notification.requestPermission(function(status) {  // status is "granted", if accepted by user
		var n = new Notification('Title', { 
			body: 'I am the body text!',
			icon: '/path/to/icon.png' // optional

Once you confirm the Notification API is supported, you must request notification access from the user.  If the status comes back as "granted", you can create a new notification with a title and body text.

Closing the notification is simple with the close method:


I really like the idea of the browser providing native APIs for common UI components, but I do find it funny that each browser has a different look for notifications.  I do like the simplicity of the Web Notifications API though -- what do you think?

Recent Features

Incredible Demos

  • By
    MooTools Window Object Dumping

    Ever want to see all of the information stored within the window property of your browser? Here's your chance. The XHTML We need a wrapper DIV that we'll consider a console. The CSS I like making this look like a command-line console. The MooTools JavaScript Depending on what you have loaded...

  • By
    Duplicate the jQuery Homepage Tooltips Using MooTools

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: Here's how to accomplish this same effect using MooTools. The XHTML The above XHTML was taken directly from the jQuery homepage -- no changes. The CSS The above CSS has been slightly modified to match the CSS rules already...


  1. The things you can do with JS. This one I didn’t know. Thanks for sharing!! You can also add an image to it as well, right?

  2. new Notification(
            body : 'This is a foo notif',
            icon : 'path/to/icon.png'

    More options (including a handy rtl switch): https://developer.mozilla.org/en-US/docs/Web/API/notification

    • Ahh! Great! :D Thanks Mitchell!

  3. Andrew

    This would be amazing if this already worked on all mobile devices — using something like phone gap to wrap your JS application – you could use notifications with this built-in API :) Much easier than a lot of the hoops you have to jump through now.


  4. W3C did well by putting together this JS code for web notification. I wonder if this would work across all the browsers?

  5. David

    Hmmmm…. Interesting. I’ll show this feature to our marketing department. They allways got some creative thoughts and come up with what to do

  6. Oleh

    Hi David, nice article! I made a little wrapper for window.Notification, maybe you can help make it better.

  7. Interesting. The notifications time-out and disappear for Firefox (at least in 36.0.1) on Windows (and they have a megaphone icon), but for Chrome 40, there’s no icon, and they never disappear by themselves (only by clicking on the close button).

  8. yachris

    One slightly awful thing about this — Firefox saw fit to put the notification on *my other monitor*, probably because I have Firefox full-screen’d on this monitor. It seemed to disappear really fast too.

    As Ted Young writes above, it’s kind of unfortunate that you don’t get any control over when (and if!) it disappears. I’ve used the Pines Notify library successfully. It’s also quite easy to style to match your web site’s look.

  9. Interesting – I was clicking away not sure what was supposed to be happening – dual monitor setup and the notifications appear top right on my right hand monitor as opposed to top right of the monitor that the browser is in. On a Mac.

  10. Interesting. The notifications time-out and disappear for Firefox (at least in 36.0.1) on Windows (and they have a megaphone icon), but for Chrome 40, there’s no icon, and they never disappear by themselves (only by clicking on the close button).

  11. NO!!! I cannot use it because IE does not support it. Bummer!!!

  12. SB

    Oh… The web 2.0 (are we still on 2.0?)… the next desktop! :)
    PS: I knew things started changing the minute I saw Quake 3 ported to javascript inside a browser.

  13. How do I implement this server side, say in my live website?

  14. Jack

    Doesn’t work on Android Chrome browser… :-(

  15. Hi David,
    I would like to show notification without any browser open if new post or article published.
    Is it possible in this api?
    How can we do without any browser open ?

  16. Roberto

    Hi David,
    This notifications don´t work in chrome (android).
    You can try it with the ‘demo’ with this page.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!