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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

Incredible Demos


  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.

  19. Miguel

    Any idea on how to run this in IE8+?

  20. Alexandra

    Hi, I made also a web notification that works pretty alike, I was trying to find a way to get rid of the button, so the notification of the subscribing will be activated only for entering in the web site, do you know how to accomplish this?


  21. ahmads

    The Notification permission may only be requested from inside a short running user-generated event handler.

    in mozila browser, not showing pop request permission.

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