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

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

Incredible Demos

  • By
    Introducing MooTools ScrollSpy

    I've been excited to release this plugin for a long time. MooTools ScrollSpy is a unique but simple MooTools plugin that listens to page scrolling and fires events based on where the user has scrolled to in the page. Now you can fire specific...

  • By
    Implement jQuery’s hover() Method in MooTools

    jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other...


  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.

  17. How to make the Web Notification API realtime running on my website?
    Do you have any suggestions? Thanks!

  18. shamshad

    Hi ,

    i want to create a notification like facebook, while user not open any website but notification come after time to time.if user one time allow notification.

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