npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

browser-notifications

v0.0.3

Published

Browser notifications with a nice promisified API

Readme

Browser notifications

Synopsis

This library provides a promise-based wrapper on top of the native implementation, intended to be simpler and safer to use. The developer can choose to use the browser built-in (native) promises, or inject any other A+ promise implementation.

Motivation

Modern browsers support the Notifications API defined by the WHATWG. The API is pretty small and well documentated and not too complex. There are however a few things that can go wrong, and race conditions can occur if the developer isn't careful. One example is the is isPermitted() function which in this library returns a Promise. The core logic is strictly synchronous, so one might wonder why it needs to be promisified? The reason is that there might be an ongoing request for permission while running isPermitted(), which in this case will be awaited. The native implementation leaves all such control flow handling to the developer.

API

Require

browser-notifications is required as any other CommonJS package:

var browserNotifications = require( 'browser-notifications' );

or inject your favorite promise implementation into it, to ensure that

  • It works on browsers which don't have native Promises
  • The promises you get back have the functionality you desire
var Promise = require( 'bluebird' );
var browserNotifications = require( 'browser-notifications' )( Promise );

Check browser support

isSupported( ) // -> Boolean

Check and request for permission

isPermitted( ) eventually returns either a Boolean whether permission is granted (true) or denied (false), or null meaning it still is unknown. Calling this function will not cause a dialog to appear in the browser, it is simply a passive call.

isPermitted( ) // -> Promise{ Boolean || null }

requestPermissions( ) should be called if you want to show notifications, either because isPermitted( ) hinted this wasn't already permitted, or because you simply want to try. First time, the browser will show a little dialog to ask the user for permission for the given site.

The function returns the same as isPermitted( ) with the same semantics.

requestPermissions( ) // Promise{ Boolean || null }

Show a notification

There are two functions for showing a notification. The bare create( ) and the easier (and preferred) send( ). They take the same arguments.

The body, iconUrl and timeout arguments are optional, but need to exist in the same order, i.e. if only timeout is specified, the other must be specified too, as null.

For send( ), timeout will be enforced if the browser doesn't support the 'close' event for notifications. Otherwise, the returned Promise would never be resolved. This is because some browsers automatically will close the notification after a certain timeout.

create( title [, body [, iconUrl [, timeout ] ] ] ); // -> Notification object
send( title [, body [, iconUrl [, timeout ] ] ] ); // -> Promise{ Boolean }

The send( ) function returns a Promise which will be resolved to true if the notification was clicked, and false if it timed out (and dissapeared). It will be rejected if there was an error with this notification.

Example

var Promise = require( 'bluebird' );
var browserNotifications = require( 'browser-notifications' )( Promise );

if ( browserNotifications.isSupported( ) )
{
	browserNotifications.requestPermissions( )
	.then( function( isPermitted ) {
		if ( isPermitted )
			return browserNotifications.send( "My title", "My body" )
			.then( function( wasClicked ) {
				console.log( "The notification was clicked: ", wasClicked );
			} );
		else
			console.log( "We asked for permission, but got denied" );
	} )
	.catch( function( err ) {
		console.error( "An error occured", err );
	} );
}