Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-web-notification

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-web-notification

Web Notifications made easy.

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

simple-web-notification

NPM Version CI Coverage Status Known Vulnerabilities Inline docs License

Web Notifications made easy

Overview

The simple-web-notification is a simplified web notifications API with automatic permissions support.

This library requires no external dependencies, however the browser must support the Notification API or have a polyfill available.

See W3 Specification for more information.

Demo

Live Demo

Usage

In order to use the simplified web notification API you first must add the relevant dependencies:

<script type="text/javascript" src="web-notification.js"></script>

Now you can use the API anywhere in your application, for example:

document.querySelector('.some-button').addEventListener('click', function onClick() {
    webNotification.showNotification('Example Notification', {
        body: 'Notification Text...',
        icon: 'my-icon.ico',
        onClick: function onNotificationClicked() {
            console.log('Notification clicked.');
        },
        autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
    }, function onShow(error, hide) {
        if (error) {
            window.alert('Unable to show notification: ' + error.message);
        } else {
            console.log('Notification Shown.');

            setTimeout(function hideNotification() {
                console.log('Hiding notification....');
                hide(); //manually close the notification (you can skip this if you use the autoClose option)
            }, 5000);
        }
    });
});

In case you wish to use service worker web notifications, you must provide the serviceWorkerRegistration in the options as follows:

navigator.serviceWorker.register('service-worker.js').then(function(registration) {
    document.querySelector('.some-button').addEventListener('click', function onClick() {
        webNotification.showNotification('Example Notification', {
            serviceWorkerRegistration: registration,
            body: 'Notification Text...',
            icon: 'my-icon.ico',
            actions: [
                {
                    action: 'Start',
                    title: 'Start'
                },
                {
                    action: 'Stop',
                    title: 'Stop'
                }
            ],
            autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
        }, function onShow(error, hide) {
            if (error) {
                window.alert('Unable to show notification: ' + error.message);
            } else {
                console.log('Notification Shown.');

                setTimeout(function hideNotification() {
                    console.log('Hiding notification....');
                    hide(); //manually close the notification (you can skip this if you use the autoClose option)
                }, 5000);
            }
        });
    });
});

In case you wish to invoke the permissions API manually you can use the webNotification.requestPermission function.
This function triggers the request permissions dialog in case permissions were not already granted.

//manually ask for notification permissions (invoked automatically if needed and allowRequest=true)
webNotification.requestPermission(function onRequest(granted) {
    if (granted) {
        console.log('Permission Granted.');
    } else {
        console.log('Permission Not Granted.');
    }
});

When using an AMD loader (such as RequireJS) or CommonJS type loader, the webNotification object is not automatically defined on the window scope.

Installation

Run npm install in your project as follows:

npm install --save simple-web-notification

Or if you are using bower, you can install it as follows:

bower install simple-web-notification --save

Limitations

The web notifications API is not fully supported in all browsers.

Please see supported browser versions for more information on the official spec support.

API Documentation

See full docs at: API Docs

Contributing

See contributing guide

Release History

DateVersionDescription
2020-05-13v2.0.1Revert bower.json deletion but not use it in CI build
2020-05-11v2.0.0Migrate to github actions, upgrade minimal node version and remove bower
2019-02-08v1.0.32Maintenance
2018-06-25v1.0.28Expose webNotification.requestPermission #5
2018-06-14v1.0.26Better error detection on chrome mobile #4
2017-08-25v1.0.21Support service worker web notifications
2017-01-31v1.0.3Removed polyfill dependency
2017-01-22v1.0.0Official release
2017-01-22v0.0.2Initial release

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

Keywords

FAQs

Package last updated on 13 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc