├── .config
└── karma.conf.js
├── .editorconfig
├── .eslintrc.js
├── .gitattributes
├── .github
├── CONTRIBUTING.md
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
└── workflows
│ └── ci.yml
├── .gitignore
├── .jsbeautifyrc
├── .npmignore
├── .stylelintrc.json
├── LICENSE
├── README.md
├── angular-web-notification.js
├── bower.json
├── docs
├── CHANGELOG.md
├── api.md
├── example
│ ├── example.css
│ └── example.js
├── index.html
└── service-worker.js
├── inch.json
├── package.json
└── test
├── helpers
└── notify-mock.js
└── spec
└── angular-web-notification-spec.js
/.config/karma.conf.js:
--------------------------------------------------------------------------------
1 | /*global module: false, require: false */
2 |
3 | module.exports = function (config) {
4 | 'use strict';
5 |
6 | const mainJSFile = require('../package.json').main;
7 |
8 | config.set({
9 | basePath: '../',
10 | frameworks: [
11 | 'mocha',
12 | 'sinon-chai'
13 | ],
14 | port: 9876,
15 | logLevel: config.LOG_INFO,
16 | autoWatch: false,
17 | browsers: [
18 | 'ChromiumHeadless'
19 | ],
20 | singleRun: false,
21 | reporters: [
22 | 'progress',
23 | 'coverage'
24 | ],
25 | preprocessors: {
26 | [mainJSFile]: [
27 | 'coverage'
28 | ]
29 | },
30 | coverageReporter: {
31 | dir: 'coverage',
32 | reporters: [
33 | {
34 | type: 'lcov',
35 | subdir: '.'
36 | }
37 | ],
38 | check: {
39 | global: {
40 | statements: 100,
41 | functions: 100,
42 | lines: 100,
43 | branches: 100
44 | }
45 | }
46 | },
47 | customLaunchers: {
48 | ChromeHeadlessCI: {
49 | base: 'ChromeHeadless',
50 | flags: [
51 | '--no-sandbox'
52 | ]
53 | }
54 | },
55 | files: [
56 | '**/jquery/dist/jquery.js',
57 | '**/angular/angular.js',
58 | '**/angular-mocks/angular-mocks.js',
59 | 'test/helpers/**/*.js',
60 | '**/simple-web-notification/web-notification.js',
61 | mainJSFile,
62 | 'test/spec/**/*.js'
63 | ]
64 | });
65 | };
66 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 |
2 | root = true
3 |
4 | [*]
5 | charset = utf-8
6 | end_of_line = lf
7 | insert_final_newline = true
8 | trim_trailing_whitespace = true
9 | indent_style = space
10 | indent_size = 4
11 |
12 | [*.json]
13 | indent_size = 2
14 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | 'env': {
3 | 'browser': true,
4 | 'commonjs': true,
5 | 'es2021': true,
6 | 'mocha': true
7 | },
8 | 'globals': {
9 | 'angular': 'readonly'
10 | },
11 | 'extends': 'eslint:recommended',
12 | 'parserOptions': {
13 | 'ecmaVersion': 13
14 | },
15 | 'rules': {
16 | 'indent': [
17 | 'error',
18 | 4
19 | ],
20 | 'linebreak-style': [
21 | 'error',
22 | 'unix'
23 | ],
24 | 'quotes': [
25 | 'error',
26 | 'single'
27 | ],
28 | 'semi': [
29 | 'error',
30 | 'always'
31 | ]
32 | }
33 | };
34 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text eol=lf
2 |
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contribution Guidelines
2 |
3 |
4 | ## Issues
5 |
6 | Found a bug? Got a question? Want some enhancement?
7 | First place to go is the repository issues section, and I'll try to help as much as possible.
8 |
9 | ## Pull Requests
10 |
11 | Fixed a bug or just want to provided additional functionality?
12 | Simply fork this repository, implement your changes and create a pull request.
13 | Few guidelines regarding pull requests:
14 |
15 | * This repository is integrated with github actions for continuous integration.
16 |
17 | Your pull request build must pass (the build will run automatically).
18 | You can run the following command locally to ensure the build will pass:
19 |
20 | ````sh
21 | npm test
22 | ````
23 |
24 | * This library is using multiple code inspection tools to validate certain level of standards. The configuration is part of the repository and you can set your favorite IDE using that configuration. You can run the following command locally to ensure the code inspection passes:
25 |
26 | ````sh
27 | npm run lint
28 | ````
29 |
30 | * There are many automatic unit tests as part of the library which provide full coverage of the functionality. Any fix/enhancement must come with a set of tests to ensure it's working well.
31 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug Report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: sagiegurari
7 |
8 | ---
9 |
10 | ### Describe The Bug
11 |
12 |
13 | ### To Reproduce
14 |
15 |
16 | ### Error Stack
17 |
18 | ```console
19 | The error stack trace
20 | ```
21 |
22 | ### Code Sample
23 |
24 | ```js
25 | // paste code here
26 | ```
27 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature Request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: sagiegurari
7 |
8 | ---
9 |
10 | ### Feature Description
11 |
12 |
13 | ### Describe The Solution You'd Like
14 |
15 |
16 | ### Code Sample
17 |
18 | ```js
19 | // paste code here
20 | ```
21 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on: [push, pull_request]
3 | env:
4 | CLICOLOR_FORCE: 1
5 | jobs:
6 | ci:
7 | name: CI
8 | runs-on: ubuntu-latest
9 | strategy:
10 | fail-fast: false
11 | steps:
12 | - name: Checkout
13 | uses: actions/checkout@v2
14 | - name: Install Chrome
15 | uses: browser-actions/setup-chrome@latest
16 | - name: Install Dependencies
17 | run: npm install
18 | - name: Run CI
19 | run: npm test
20 | - name: Coveralls
21 | uses: coverallsapp/github-action@master
22 | with:
23 | github-token: ${{ secrets.GITHUB_TOKEN }}
24 | path-to-lcov: './coverage/lcov.info'
25 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .temp
3 | node_modules
4 | bower_components
5 | npm-debug.log
6 | package-lock.json
7 | .nyc_output
8 | coverage
9 |
--------------------------------------------------------------------------------
/.jsbeautifyrc:
--------------------------------------------------------------------------------
1 | {
2 | "js": {
3 | "indent_size": 4,
4 | "indent_char": " ",
5 | "eol": "\n",
6 | "indent_level": 0,
7 | "indent_with_tabs": false,
8 | "preserve_newlines": true,
9 | "max_preserve_newlines": 2,
10 | "space_in_paren": false,
11 | "jslint_happy": true,
12 | "space_after_anon_function": true,
13 | "brace_style": "collapse",
14 | "break_chained_methods": false,
15 | "keep_array_indentation": true,
16 | "unescape_strings": false,
17 | "wrap_line_length": 0,
18 | "end_with_newline": true,
19 | "comma_first": false,
20 | "eval_code": false,
21 | "keep_function_indentation": false,
22 | "space_before_conditional": true,
23 | "good_stuff": true
24 | },
25 | "css": {
26 | "indent_size": 2,
27 | "indent_char": " ",
28 | "indent_with_tabs": false,
29 | "eol": "\n",
30 | "end_with_newline": true,
31 | "selector_separator_newline": false,
32 | "newline_between_rules": true
33 | },
34 | "html": {
35 | "indent_size": 4,
36 | "indent_char": " ",
37 | "indent_with_tabs": false,
38 | "eol": "\n",
39 | "end_with_newline": true,
40 | "preserve_newlines": true,
41 | "max_preserve_newlines": 2,
42 | "indent_inner_html": true,
43 | "brace_style": "collapse",
44 | "indent_scripts": "normal",
45 | "wrap_line_length": 0,
46 | "wrap_attributes": "auto",
47 | "wrap_attributes_indent_size": 4
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | target
2 | .temp
3 | .nyc_output
4 | .config
5 | coverage
6 | bower_components
7 | .github
8 | test
9 | example
10 |
--------------------------------------------------------------------------------
/.stylelintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "stylelint-config-standard"
3 | }
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # angular-web-notification
2 |
3 | [](https://www.npmjs.org/package/angular-web-notification) [](https://github.com/sagiegurari/angular-web-notification/actions) [](https://coveralls.io/r/sagiegurari/angular-web-notification) [](https://snyk.io/test/github/sagiegurari/angular-web-notification) [](http://inch-ci.org/github/sagiegurari/angular-web-notification) [](https://github.com/sagiegurari/angular-web-notification/blob/master/LICENSE)
4 |
5 | > Web Notifications AngularJS Service
6 |
7 | * [Overview](#overview)
8 | * [Demo](https://sagiegurari.github.io/angular-web-notification/)
9 | * [Usage](#usage)
10 | * [Installation](#installation)
11 | * [Limitations](#limitations)
12 | * [API Documentation](docs/api.md)
13 | * [Contributing](.github/CONTRIBUTING.md)
14 | * [Release History](#history)
15 | * [License](#license)
16 |
17 |
18 | ## Overview
19 | The angular-web-notification is an angular service wrapper for the web notifications API.
20 |
21 | It is using the [simple-web-notification](https://github.com/sagiegurari/simple-web-notification) library which provides a simple and easy notification API which works across browsers and provides automatic permission handling.
22 |
23 | See [W3 Specification](https://dvcs.w3.org/hg/notifications/raw-file/tip/Overview.html) and [simple-web-notification](https://github.com/sagiegurari/simple-web-notification) for more information.
24 |
25 | ### Angular 2 and Up
26 | For angular 2 and above, it is recommanded to use the [simple-web-notification](https://github.com/sagiegurari/simple-web-notification) library directly.
27 | It provides the same API and it is not dependend on angular.
28 |
29 | ## Demo
30 | [Live Demo](https://sagiegurari.github.io/angular-web-notification/)
31 |
32 |
33 | ## Usage
34 | In order to use the angular service you first must add the relevant dependencies:
35 |
36 | ```html
37 |
38 |
39 |
40 | ```
41 |
42 | Next you must define it as a dependency in your main angular module as follows:
43 |
44 | ```js
45 | angular.module('exampleApp', [
46 | 'angular-web-notification'
47 | ]);
48 | ```
49 |
50 | Now you can inject and use the service into your modules (directives/services/...), for example:
51 |
52 | ```js
53 | angular.module('exampleApp').directive('showButton', ['webNotification', function (webNotification) {
54 | return {
55 | ...
56 | link: function (scope, element) {
57 | element.on('click', function onClick() {
58 | webNotification.showNotification('Example Notification', {
59 | body: 'Notification Text...',
60 | icon: 'my-icon.ico',
61 | onClick: function onNotificationClicked() {
62 | console.log('Notification clicked.');
63 | },
64 | autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
65 | }, function onShow(error, hide) {
66 | if (error) {
67 | window.alert('Unable to show notification: ' + error.message);
68 | } else {
69 | console.log('Notification Shown.');
70 |
71 | setTimeout(function hideNotification() {
72 | console.log('Hiding notification....');
73 | hide(); //manually close the notification (you can skip this if you use the autoClose option)
74 | }, 5000);
75 | }
76 | });
77 | });
78 | }
79 | };
80 | }]);
81 | ```
82 |
83 | In case you wish to use service worker web notifications, you must provide the serviceWorkerRegistration in the options as follows:
84 |
85 | ````js
86 | //Get the service worker registeration object at the startup of the application.
87 | //This is an aysnc operation so you should not try to use it before the promise is finished.
88 | var serviceWorkerRegistration;
89 | navigator.serviceWorker.register('service-worker.js').then(function(registration) {
90 | serviceWorkerRegistration = registration;
91 | });
92 |
93 | //when setting on even handlers in different areas of the application, use that registration object instance (must be done after the registration is available)
94 | element.on('click', function onClick() {
95 | webNotification.showNotification('Example Notification', {
96 | serviceWorkerRegistration: serviceWorkerRegistration,
97 | body: 'Notification Text...',
98 | icon: 'my-icon.ico',
99 | actions: [
100 | {
101 | action: 'Start',
102 | title: 'Start'
103 | },
104 | {
105 | action: 'Stop',
106 | title: 'Stop'
107 | }
108 | ],
109 | autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
110 | }, function onShow(error, hide) {
111 | if (error) {
112 | window.alert('Unable to show notification: ' + error.message);
113 | } else {
114 | console.log('Notification Shown.');
115 |
116 | setTimeout(function hideNotification() {
117 | console.log('Hiding notification....');
118 | hide(); //manually close the notification (you can skip this if you use the autoClose option)
119 | }, 5000);
120 | }
121 | });
122 | });
123 | ````
124 |
125 |
126 | ## Installation
127 | Run npm install in your project as follows:
128 |
129 | ```sh
130 | npm install --save angular-web-notification
131 | ```
132 |
133 | Or if you are using bower, you can install it as follows:
134 |
135 | ```sh
136 | bower install angular-web-notification --save
137 | ```
138 |
139 |
140 | ## Limitations
141 | The web notifications API is not fully supported in all browsers.
142 |
143 | Please see [supported browser versions](http://caniuse.com/#feat=notifications) for more information on the official spec support.
144 |
145 | ## API Documentation
146 | See full docs at: [API Docs](docs/api.md)
147 |
148 | ## Contributing
149 | See [contributing guide](.github/CONTRIBUTING.md)
150 |
151 |
152 | ## Release History
153 |
154 | | Date | Version | Description |
155 | | ----------- | ------- | ----------- |
156 | | 2020-05-13 | v2.0.1 | Revert bower.json deletion but not use it in CI build |
157 | | 2020-05-11 | v2.0.0 | Migrate to github actions, upgrade minimal node version and remove bower |
158 | | 2019-02-08 | v1.2.31 | Maintenance |
159 | | 2017-08-25 | v1.2.24 | Document support of service worker web notifications |
160 | | 2017-01-22 | v1.2.0 | Split the internal web notification API into a new project: simple-web-notification |
161 | | 2016-11-23 | v1.0.19 | Use forked version of html5-desktop-notifications in order to resolve few issues |
162 | | 2016-11-04 | v1.0.16 | Upgrading to html5-desktop-notifications 3.0.0 |
163 | | 2016-09-10 | v1.0.6 | Default to website favicon.ico if icon not provided in options |
164 | | 2016-09-07 | v1.0.4 | Callback is now optional |
165 | | 2016-06-14 | v0.0.78 | Published via NPM (in addition to bower) |
166 | | 2016-03-08 | v0.0.65 | Added webNotification.permissionGranted attribute |
167 | | 2015-09-26 | v0.0.31 | Update bower dependencies |
168 | | 2015-09-26 | v0.0.30 | Added 'onClick' option to enable adding onclick event handler for the notification |
169 | | 2015-08-16 | v0.0.22 | uglify fix |
170 | | 2015-02-16 | v0.0.7 | Automatic unit tests via karma |
171 | | 2015-02-05 | v0.0.5 | Doc changes |
172 | | 2014-12-09 | v0.0.3 | API now enables/disables the capability to automatically request for permissions needed to display the notification. |
173 | | 2014-12-08 | v0.0.2 | Initial release |
174 |
175 |
176 | ## License
177 | Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.
178 |
--------------------------------------------------------------------------------
/angular-web-notification.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @ngdoc method
3 | * @function
4 | * @memberof! webNotification
5 | * @alias webNotification.initWebNotification
6 | * @private
7 | *
8 | * @description
9 | * Initializes the angular web notification service.
10 | *
11 | * @param {Object} webNotificationAPI - The simplified web notification API
12 | */
13 | (function initWebNotification(webNotificationAPI) {
14 | 'use strict';
15 |
16 | const webNotification = window.angular.module('angular-web-notification', []);
17 |
18 | /**
19 | * @ngdoc service
20 | * @name webNotification
21 | * @namespace webNotification
22 | * @author Sagie Gur-Ari
23 | * @returns {Object} The service instance
24 | *
25 | * @description
26 | * The web notification service wraps the HTML 5 Web Notifications API as an angular service.
27 | * See [simple-web-notification](https://github.com/sagiegurari/simple-web-notification/blob/master/docs/api.md) for more API details.
28 | */
29 | webNotification.factory('webNotification', function onCreateService() {
30 | /**
31 | * Shows the notification based on the provided input.
32 | * The callback invoked will get an error object (in case of an error, null in
33 | * case of no errors) and a 'hide' function which can be used to hide the notification.
34 | *
35 | * @function
36 | * @memberof! webNotification
37 | * @alias webNotification.showNotification
38 | * @public
39 | * @param {String} [title] - The notification title text (defaulted to empty string if null is provided)
40 | * @param {Object} [options] - Holds the notification data (web notification API spec for more info)
41 | * @param {String} [options.icon=/favicon.ico] - The notification icon (defaults to the website favicon.ico)
42 | * @param {Number} [options.autoClose] - Auto closes the notification after the provided amount of millies (0 or undefined for no auto close)
43 | * @param {function} [options.onClick] - An optional onclick event handler
44 | * @param {ShowNotificationCallback} [callback] - Called after the show is handled.
45 | * @example
46 | * ```js
47 | * //show web notification when button is clicked
48 | * webNotification.showNotification('Example Notification', {
49 | * body: 'Notification Text...',
50 | * icon: 'my-icon.ico',
51 | * onClick: function onNotificationClicked() {
52 | * console.log('Notification clicked.');
53 | * },
54 | * autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
55 | * }, function onShow(error, hide) {
56 | * if (error) {
57 | * window.alert('Unable to show notification: ' + error.message);
58 | * } else {
59 | * console.log('Notification Shown.');
60 | *
61 | * setTimeout(function hideNotification() {
62 | * console.log('Hiding notification....');
63 | * hide(); //manually close the notification (you can skip this if you use the autoClose option)
64 | * }, 5000);
65 | * }
66 | * });
67 | *
68 | * //service worker example
69 | * navigator.serviceWorker.register('service-worker.js').then(function(registration) {
70 | * webNotification.showNotification('Example Notification', {
71 | * serviceWorkerRegistration: registration,
72 | * body: 'Notification Text...',
73 | * icon: 'my-icon.ico',
74 | * actions: [
75 | * {
76 | * action: 'Start',
77 | * title: 'Start'
78 | * },
79 | * {
80 | * action: 'Stop',
81 | * title: 'Stop'
82 | * }
83 | * ],
84 | * autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
85 | * }, function onShow(error, hide) {
86 | * if (error) {
87 | * window.alert('Unable to show notification: ' + error.message);
88 | * } else {
89 | * console.log('Notification Shown.');
90 | *
91 | * setTimeout(function hideNotification() {
92 | * console.log('Hiding notification....');
93 | * hide(); //manually close the notification (you can skip this if you use the autoClose option)
94 | * }, 5000);
95 | * }
96 | * });
97 | * });
98 | * ```
99 | */
100 | const showNotification = webNotificationAPI.showNotification;
101 |
102 | if (showNotification) {
103 | return webNotificationAPI;
104 | }
105 | });
106 | }(window.webNotification));
107 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular-web-notification",
3 | "version": "2.0.1",
4 | "description": "AngularJS service for displaying web notifications.",
5 | "authors": [
6 | "Sagie Gur-Ari "
7 | ],
8 | "license": "Apache-2.0",
9 | "homepage": "http://github.com/sagiegurari/angular-web-notification",
10 | "keywords": [
11 | "AngularJS",
12 | "notifications",
13 | "web notifications"
14 | ],
15 | "main": "angular-web-notification.js",
16 | "ignore": [
17 | "node_modules",
18 | "bower_components",
19 | ".github",
20 | "project",
21 | "test",
22 | "tests",
23 | "example",
24 | "target",
25 | ".travis.yml",
26 | ".atom.*.yml",
27 | "Gruntfile.js"
28 | ],
29 | "dependencies": {
30 | "simple-web-notification": "latest",
31 | "angular": "~1"
32 | },
33 | "devDependencies": {
34 | "jquery": "latest",
35 | "angular-mocks": "~1"
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/docs/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | | Date | Version | Description |
2 | | ----------- | ------- | ----------- |
3 | | 2020-05-13 | v2.0.1 | Revert bower.json deletion but not use it in CI build |
4 | | 2020-05-11 | v2.0.0 | Migrate to github actions, upgrade minimal node version and remove bower |
5 | | 2019-02-08 | v1.2.31 | Maintenance |
6 | | 2017-08-25 | v1.2.24 | Document support of service worker web notifications |
7 | | 2017-01-22 | v1.2.0 | Split the internal web notification API into a new project: simple-web-notification |
8 | | 2016-11-23 | v1.0.19 | Use forked version of html5-desktop-notifications in order to resolve few issues |
9 | | 2016-11-04 | v1.0.16 | Upgrading to html5-desktop-notifications 3.0.0 |
10 | | 2016-09-10 | v1.0.6 | Default to website favicon.ico if icon not provided in options |
11 | | 2016-09-07 | v1.0.4 | Callback is now optional |
12 | | 2016-06-14 | v0.0.78 | Published via NPM (in addition to bower) |
13 | | 2016-03-08 | v0.0.65 | Added webNotification.permissionGranted attribute |
14 | | 2015-09-26 | v0.0.31 | Update bower dependencies |
15 | | 2015-09-26 | v0.0.30 | Added 'onClick' option to enable adding onclick event handler for the notification |
16 | | 2015-08-16 | v0.0.22 | uglify fix |
17 | | 2015-02-16 | v0.0.7 | Automatic unit tests via karma |
18 | | 2015-02-05 | v0.0.5 | Doc changes |
19 | | 2014-12-09 | v0.0.3 | API now enables/disables the capability to automatically request for permissions needed to display the notification. |
20 | | 2014-12-08 | v0.0.2 | Initial release |
21 |
--------------------------------------------------------------------------------
/docs/api.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | ## webNotification ⇒ Object
4 | The web notification service wraps the HTML 5 Web Notifications API as an angular service.
5 | See [simple-web-notification](https://github.com/sagiegurari/simple-web-notification/blob/master/docs/api.md) for more API details.
6 |
7 | **Kind**: global namespace
8 | **Returns**: Object - The service instance
9 | **Ngdoc**: service
10 | **Author**: Sagie Gur-Ari
11 |
12 |
13 | ### webNotification.showNotification([title], [options], [callback])
14 | Shows the notification based on the provided input.
15 | The callback invoked will get an error object (in case of an error, null in
16 | case of no errors) and a 'hide' function which can be used to hide the notification.
17 |
18 | **Access**: public
19 |
20 | | Param | Type | Default | Description |
21 | | --- | --- | --- | --- |
22 | | [title] | String | | The notification title text (defaulted to empty string if null is provided) |
23 | | [options] | Object | | Holds the notification data (web notification API spec for more info) |
24 | | [options.icon] | String | /favicon.ico | The notification icon (defaults to the website favicon.ico) |
25 | | [options.autoClose] | Number | | Auto closes the notification after the provided amount of millies (0 or undefined for no auto close) |
26 | | [options.onClick] | function | | An optional onclick event handler |
27 | | [callback] | ShowNotificationCallback | | Called after the show is handled. |
28 |
29 | **Example**
30 | ```js
31 | //show web notification when button is clicked
32 | webNotification.showNotification('Example Notification', {
33 | body: 'Notification Text...',
34 | icon: 'my-icon.ico',
35 | onClick: function onNotificationClicked() {
36 | console.log('Notification clicked.');
37 | },
38 | autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
39 | }, function onShow(error, hide) {
40 | if (error) {
41 | window.alert('Unable to show notification: ' + error.message);
42 | } else {
43 | console.log('Notification Shown.');
44 |
45 | setTimeout(function hideNotification() {
46 | console.log('Hiding notification....');
47 | hide(); //manually close the notification (you can skip this if you use the autoClose option)
48 | }, 5000);
49 | }
50 | });
51 |
52 | //service worker example
53 | navigator.serviceWorker.register('service-worker.js').then(function(registration) {
54 | webNotification.showNotification('Example Notification', {
55 | serviceWorkerRegistration: registration,
56 | body: 'Notification Text...',
57 | icon: 'my-icon.ico',
58 | actions: [
59 | {
60 | action: 'Start',
61 | title: 'Start'
62 | },
63 | {
64 | action: 'Stop',
65 | title: 'Stop'
66 | }
67 | ],
68 | autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
69 | }, function onShow(error, hide) {
70 | if (error) {
71 | window.alert('Unable to show notification: ' + error.message);
72 | } else {
73 | console.log('Notification Shown.');
74 |
75 | setTimeout(function hideNotification() {
76 | console.log('Hiding notification....');
77 | hide(); //manually close the notification (you can skip this if you use the autoClose option)
78 | }, 5000);
79 | }
80 | });
81 | });
82 | ```
83 |
--------------------------------------------------------------------------------
/docs/example/example.css:
--------------------------------------------------------------------------------
1 | .notification-form {
2 | padding: 10px;
3 | width: 60%;
4 | }
5 |
--------------------------------------------------------------------------------
/docs/example/example.js:
--------------------------------------------------------------------------------
1 | /*global console: false */
2 | /*jslint unparam: true*/
3 |
4 | window.angular.module('exampleApp', [
5 | 'angular-web-notification'
6 | ]).controller('exampleForm', ['$scope', function ($scope) {
7 | 'use strict';
8 |
9 | $scope.title = 'Example Notification';
10 | $scope.text = 'This is some notification text.';
11 | }]).directive('showButton', ['webNotification', function (webNotification) {
12 | 'use strict';
13 |
14 | var serviceWorkerRegistration;
15 |
16 | if (navigator.serviceWorker) {
17 | navigator.serviceWorker.register('service-worker.js').then(function (registration) {
18 | serviceWorkerRegistration = registration;
19 | });
20 | }
21 |
22 | return {
23 | restrict: 'C',
24 | scope: {
25 | notificationTitle: '=',
26 | notificationText: '='
27 | },
28 | link: function (scope, element) {
29 | element.on('click', function onClick() {
30 | webNotification.showNotification(scope.notificationTitle, {
31 | serviceWorkerRegistration: serviceWorkerRegistration,
32 | body: scope.notificationText,
33 | onClick: function onNotificationClicked() {
34 | console.log('Notification clicked.');
35 | },
36 | autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
37 | }, function onShow(error, hide) {
38 | if (error) {
39 | window.alert('Unable to show notification: ' + error.message);
40 | } else {
41 | console.log('Notification Shown.');
42 |
43 | setTimeout(function hideNotification() {
44 | console.log('Hiding notification....');
45 | hide(); //manually close the notification (you can skip this if you use the autoClose option)
46 | }, 5000);
47 | }
48 | });
49 | });
50 | }
51 | };
52 | }]);
53 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Angular Web Notification Example
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/docs/service-worker.js:
--------------------------------------------------------------------------------
1 |
2 | self.addEventListener('install', function(event) {});
3 |
--------------------------------------------------------------------------------
/inch.json:
--------------------------------------------------------------------------------
1 | {
2 | "files": {
3 | "included": [
4 | "*.js",
5 | "lib/**/*.js",
6 | "tasks/**/*.js"
7 | ],
8 | "excluded": [
9 | "**/Gruntfile.js",
10 | "**/.eslintrc.js",
11 | "**/stylelint.config.js"
12 | ]
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular-web-notification",
3 | "version": "2.0.1",
4 | "description": "AngularJS service for displaying web notifications.",
5 | "author": {
6 | "name": "Sagie Gur-Ari",
7 | "email": "sagiegurari@gmail.com"
8 | },
9 | "license": "Apache-2.0",
10 | "homepage": "http://github.com/sagiegurari/angular-web-notification",
11 | "repository": {
12 | "type": "git",
13 | "url": "http://github.com/sagiegurari/angular-web-notification.git"
14 | },
15 | "bugs": {
16 | "url": "http://github.com/sagiegurari/angular-web-notification/issues"
17 | },
18 | "keywords": [
19 | "angularjs",
20 | "web",
21 | "notification"
22 | ],
23 | "main": "angular-web-notification.js",
24 | "directories": {
25 | "test": "test/spec",
26 | "example": "example"
27 | },
28 | "scripts": {
29 | "clean": "rm -Rf ./.nyc_output ./coverage",
30 | "format": "js-beautify --config ./.jsbeautifyrc --file ./*.js ./test/**/*.js",
31 | "lint-js": "eslint ./*.js ./test/**/*.js",
32 | "lint-css": "stylelint --allow-empty-input ./docs/**/*.css",
33 | "lint": "npm run lint-js && npm run lint-css",
34 | "jstest": "karma start --single-run",
35 | "docs": "jsdoc2md ./angular-web-notification.js > ./docs/api.md",
36 | "test": "npm run clean && npm run format && npm run lint && npm run docs && npm run jstest",
37 | "postpublish": "git fetch && git pull"
38 | },
39 | "dependencies": {
40 | "angular": "^1",
41 | "simple-web-notification": "latest"
42 | },
43 | "devDependencies": {
44 | "angular-mocks": "^1",
45 | "chai": "^4",
46 | "eslint": "^8",
47 | "jquery": "^3",
48 | "js-beautify": "^1",
49 | "jsdoc-to-markdown": "^8",
50 | "karma": "^6",
51 | "karma-chrome-launcher": "^3",
52 | "karma-coverage": "^2",
53 | "karma-mocha": "^2",
54 | "karma-sinon-chai": "^2",
55 | "mocha": "^10",
56 | "sinon": "^14",
57 | "sinon-chai": "^3",
58 | "stylelint": "^13",
59 | "stylelint-config-standard": "^22"
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/test/helpers/notify-mock.js:
--------------------------------------------------------------------------------
1 | window.Notification = (function Notification() {
2 | 'use strict';
3 |
4 | const permissionInfo = {
5 | value: null
6 | };
7 |
8 | let oncePermission;
9 |
10 | const Lib = function (title, options) {
11 | Lib.validateNotification(title, options);
12 |
13 | const self = this;
14 | self.close = function () {
15 | if (options.onClick) {
16 | self.onclick();
17 | }
18 | };
19 | };
20 |
21 | Lib.MOCK_NOTIFY = true;
22 |
23 | Object.defineProperty(Lib, 'permission', {
24 | enumerable: true,
25 | get() {
26 | return permissionInfo.value;
27 | }
28 | });
29 |
30 | Lib.requestPermission = function (callback) {
31 | if (oncePermission) {
32 | oncePermission();
33 | oncePermission = null;
34 | }
35 |
36 | setTimeout(callback, 5);
37 | };
38 |
39 | Lib.setValidationNotification = function (validateNotification) {
40 | Lib.validateNotification = validateNotification || function noop() {
41 | return undefined;
42 | };
43 | };
44 |
45 | Lib.setAllowed = function (validateNotification) {
46 | Lib.setValidationNotification(validateNotification);
47 | permissionInfo.value = 'granted';
48 | };
49 |
50 | Lib.setNotAllowed = function (validateNotification) {
51 | Lib.setValidationNotification(validateNotification);
52 | permissionInfo.value = 'not-granted';
53 | };
54 |
55 | Lib.onceRequestPermission = function (callback) {
56 | oncePermission = callback;
57 | };
58 |
59 | return Lib;
60 | }());
61 |
--------------------------------------------------------------------------------
/test/spec/angular-web-notification-spec.js:
--------------------------------------------------------------------------------
1 | /*global assert: false, inject: false */
2 |
3 | describe('angular-web-notification', function () {
4 | 'use strict';
5 |
6 | const validShowValidation = function (error, hide, done) {
7 | assert.isNull(error);
8 | assert.isFunction(hide);
9 |
10 | hide();
11 |
12 | if (done) {
13 | done();
14 | }
15 | };
16 |
17 | const errorValidation = function (error, hide, done) {
18 | assert.isDefined(error);
19 | assert.isNull(hide);
20 | done();
21 | };
22 |
23 | beforeEach(window.angular.mock.module('angular-web-notification'));
24 |
25 | it('library not defined test', inject(function ($injector) {
26 | const showNotification = window.webNotification.showNotification;
27 | delete window.webNotification.showNotification;
28 |
29 | let errorDetected = false;
30 | try {
31 | $injector.get('webNotification').$get();
32 | } catch (error) {
33 | assert.isDefined(error);
34 | errorDetected = true;
35 | }
36 |
37 | window.webNotification.showNotification = showNotification;
38 |
39 | assert.isTrue(errorDetected);
40 | }));
41 |
42 | it('init test', inject(function (webNotification) {
43 | assert.isObject(webNotification);
44 | assert.isTrue(window.Notification.MOCK_NOTIFY);
45 | assert.isTrue(webNotification.lib.MOCK_NOTIFY);
46 | }));
47 |
48 | describe('showNotification', function () {
49 | describe('allowed', function () {
50 | it('valid', function (done) {
51 | inject(function (webNotification) {
52 | webNotification.allowRequest = true;
53 | assert.isTrue(webNotification.lib.MOCK_NOTIFY);
54 |
55 | window.Notification.setAllowed(function (title, options) {
56 | assert.equal(title, 'Example Notification');
57 | assert.deepEqual(options, {
58 | body: 'Notification Text...',
59 | icon: 'my-icon.ico'
60 | });
61 | });
62 |
63 | webNotification.showNotification('Example Notification', {
64 | body: 'Notification Text...',
65 | icon: 'my-icon.ico'
66 | }, function onShow(error, hide) {
67 | validShowValidation(error, hide, done);
68 | });
69 | });
70 | });
71 |
72 | it('auto close', function (done) {
73 | inject(function (webNotification) {
74 | webNotification.allowRequest = true;
75 | assert.isTrue(webNotification.lib.MOCK_NOTIFY);
76 |
77 | window.Notification.setAllowed(function (title, options) {
78 | assert.equal(title, 'Example Notification');
79 | assert.deepEqual(options, {
80 | body: 'Notification Text...',
81 | icon: 'my-icon.ico',
82 | autoClose: 600
83 | });
84 | });
85 |
86 | webNotification.showNotification('Example Notification', {
87 | body: 'Notification Text...',
88 | icon: 'my-icon.ico',
89 | autoClose: 600
90 | }, function onShow(error, hide) {
91 | validShowValidation(error, hide, done);
92 | });
93 | });
94 | });
95 |
96 | it('first time permissions', function (done) {
97 | inject(function (webNotification) {
98 | webNotification.allowRequest = true;
99 | assert.isTrue(webNotification.lib.MOCK_NOTIFY);
100 |
101 | window.Notification.setNotAllowed(function (title, options) {
102 | assert.equal(title, 'first time');
103 | assert.deepEqual(options, {
104 | icon: '/favicon.ico'
105 | });
106 | });
107 |
108 | window.Notification.onceRequestPermission(function () {
109 | window.Notification.setAllowed(function (title, options) {
110 | assert.equal(title, 'first time');
111 | assert.deepEqual(options, {
112 | icon: '/favicon.ico'
113 | });
114 | });
115 | });
116 |
117 | webNotification.showNotification('first time', {}, function onShow(error, hide) {
118 | validShowValidation(error, hide, done);
119 | });
120 | });
121 | });
122 | });
123 |
124 | describe('not allowed', function () {
125 | it('not allowed', function (done) {
126 | inject(function (webNotification) {
127 | webNotification.allowRequest = true;
128 | assert.isTrue(webNotification.lib.MOCK_NOTIFY);
129 |
130 | window.Notification.setNotAllowed();
131 |
132 | webNotification.showNotification('not allowed', {}, function onShow(error, hide) {
133 | errorValidation(error, hide, done);
134 | });
135 | });
136 | });
137 |
138 | it('not allowed and not allowed to ask permissions', function (done) {
139 | inject(function (webNotification) {
140 | assert.isTrue(webNotification.lib.MOCK_NOTIFY);
141 |
142 | window.Notification.setNotAllowed();
143 | webNotification.allowRequest = false;
144 |
145 | webNotification.showNotification('no allowRequest', {}, function onShow(error, hide) {
146 | errorValidation(error, hide, done);
147 | });
148 | });
149 | });
150 | });
151 | });
152 | });
153 |
--------------------------------------------------------------------------------