• Stars
    star
    178
  • Rank 208,361 (Top 5 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Web Notifications AngularJS Service

angular-web-notification

NPM Version CI Coverage Status Known Vulnerabilities Inline docs License

Web Notifications AngularJS Service

Overview

The angular-web-notification is an angular service wrapper for the web notifications API.

It is using the simple-web-notification library which provides a simple and easy notification API which works across browsers and provides automatic permission handling.

See W3 Specification and simple-web-notification for more information.

Angular 2 and Up

For angular 2 and above, it is recommanded to use the simple-web-notification library directly.
It provides the same API and it is not dependend on angular.

Demo

Live Demo

Usage

In order to use the angular service you first must add the relevant dependencies:

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

Next you must define it as a dependency in your main angular module as follows:

angular.module('exampleApp', [
    'angular-web-notification'
]);

Now you can inject and use the service into your modules (directives/services/...), for example:

angular.module('exampleApp').directive('showButton', ['webNotification', function (webNotification) {
return {
    ...
    link: function (scope, element) {
        element.on('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:

//Get the service worker registeration object at the startup of the application.
//This is an aysnc operation so you should not try to use it before the promise is finished.
var serviceWorkerRegistration;
navigator.serviceWorker.register('service-worker.js').then(function(registration) {
    serviceWorkerRegistration = registration;
});

//when setting on even handlers in different areas of the application, use that registration object instance (must be done after the registration is available)
element.on('click', function onClick() {
    webNotification.showNotification('Example Notification', {
        serviceWorkerRegistration: serviceWorkerRegistration,
        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);
        }
    });
});

Installation

Run npm install in your project as follows:

npm install --save angular-web-notification

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

bower install angular-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

Date Version Description
2020-05-13 v2.0.1 Revert bower.json deletion but not use it in CI build
2020-05-11 v2.0.0 Migrate to github actions, upgrade minimal node version and remove bower
2019-02-08 v1.2.31 Maintenance
2017-08-25 v1.2.24 Document support of service worker web notifications
2017-01-22 v1.2.0 Split the internal web notification API into a new project: simple-web-notification
2016-11-23 v1.0.19 Use forked version of html5-desktop-notifications in order to resolve few issues
2016-11-04 v1.0.16 Upgrading to html5-desktop-notifications 3.0.0
2016-09-10 v1.0.6 Default to website favicon.ico if icon not provided in options
2016-09-07 v1.0.4 Callback is now optional
2016-06-14 v0.0.78 Published via NPM (in addition to bower)
2016-03-08 v0.0.65 Added webNotification.permissionGranted attribute
2015-09-26 v0.0.31 Update bower dependencies
2015-09-26 v0.0.30 Added 'onClick' option to enable adding onclick event handler for the notification
2015-08-16 v0.0.22 uglify fix
2015-02-16 v0.0.7 Automatic unit tests via karma
2015-02-05 v0.0.5 Doc changes
2014-12-09 v0.0.3 API now enables/disables the capability to automatically request for permissions needed to display the notification.
2014-12-08 v0.0.2 Initial release

License

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

More Repositories

1

cargo-make

Rust task runner and build tool.
Rust
2,341
star
2

duckscript

Simple, extendable and embeddable scripting language.
Rust
485
star
3

node-go-require

Load Google GO files as any javascript modules under nodeJS runtime.
JavaScript
154
star
4

brackets-portable

Windows portable version of adobe brackets web development editor.
HTML
130
star
5

run_script

Run shell scripts in rust.
Rust
117
star
6

simple-web-notification

Web Notifications made easy
JavaScript
71
star
7

shell2batch

Coverts simple basic shell scripts to windows batch scripts.
Rust
55
star
8

simple-oracledb

Extend capabilities of oracledb with simplified API for quicker development.
JavaScript
36
star
9

envmnt

Environment variables utility functions.
Rust
27
star
10

simple_redis

Simple and resilient redis client for rust.
Rust
26
star
11

fax4j

Java fax library.
Java
23
star
12

userscripts-ads-dom-remover

Removes Ad Containers from DOM (doesn't replace adblocker extension, but blocks dynamic content which the adblocker fails to block by removing whole sections from the HTML DOM.)
JavaScript
14
star
13

multiple-redis

Run redis commands against multiple redis instances.
JavaScript
13
star
14

ci_info

Provides current CI environment information.
Rust
12
star
15

event-emitter-enhancer

Extends the Node.js events.EventEmitter to provide additional functionality.
JavaScript
11
star
16

rust_info

Extracts the current rust compiler information.
Rust
8
star
17

fsio

File System and Path utility functions.
Rust
8
star
18

cliparser

Simple command line parser
Rust
8
star
19

c_stringfn

String functions for C.
C
7
star
20

cargo-make-tasks

Reusable makefiles for cargo-make
7
star
21

node-spider-script

Run spider script files as javascript modules in NodeJS.
JavaScript
6
star
22

grunt-markdownlint

A grunt task for markdown style checker and lint tool.
JavaScript
6
star
23

git_info

Extracts git repository information.
Rust
6
star
24

c_forever

Ensure the program runs continuously.
C
5
star
25

c_workqueue

Threaded work queue for C.
C
5
star
26

c_string_buffer

A simple string buffer for C
C
5
star
27

c_scriptexec

Invoke complex multi command scripts with a single C api call.
C
4
star
28

x2fax

The x2fax (fax4j sub project) is a set of standalone servers and deployable components which enable to provide x2fax capabilities.
Java
4
star
29

scriptexec

Run complex native scripts with a single command, similar to system commands.
R
3
star
30

oracledb-upsert

UPSERT (insert/update) extension to oracledb
JavaScript
3
star
31

angular-number-input

AngularJS number input directive.
JavaScript
3
star
32

c_json

JSON parser/writer for C
C
3
star
33

c_hs

Embeddable HTTP Server for C
C
3
star
34

node-later

Defer function calls to the start of the next cycle.
JavaScript
2
star
35

c_ini

INI parser/writer for C
C
2
star
36

c_hashtable

C Hash TAble
C
2
star
37

c_vector

A simple growable vector for C
C
2
star
38

sagiegurari.github.io

https://sagiegurari.github.io
HTML
2
star
39

c_thread_pool

Thread Pool for C
C
2
star
40

cmake-modules

Reusable cmake utilities for C projects.
CMake
2
star
41

userscripts-hackernews

Improved mobile usability and modern styling for Hackernews
JavaScript
2
star
42

c_eventemitter

Simple Event Emitter C API
C
2
star
43

c_fsio

File System utility functions.
C
2
star
44

c_thread_lock

Simple Thread Lock/Signal for C
C
2
star
45

funcs-js

Function wrappers for enhanced behavior.
JavaScript
2
star
46

misc-test

misc tests
1
star
47

js-project-commons

Common web and node.js grunt tasks/lint configs/md templates and so on...
JavaScript
1
star
48

docker-cargo-make

Docker image with Rust and cargo-make preinstalled.
Dockerfile
1
star