Skip to content

Commit ec4fae4

Browse files
committed
Expose webNotification.requestPermission #5
1 parent ba5de64 commit ec4fae4

File tree

9 files changed

+186
-25
lines changed

9 files changed

+186
-25
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $('.some-button').on('click', function onClick() {
6262

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

65-
````js
65+
```js
6666
navigator.serviceWorker.register('service-worker.js').then(function(registration) {
6767
$('.some-button').on('click', function onClick() {
6868
webNotification.showNotification('Example Notification', {
@@ -94,7 +94,21 @@ navigator.serviceWorker.register('service-worker.js').then(function(registration
9494
});
9595
});
9696
});
97-
````
97+
```
98+
99+
In case you wish to invoke the permissions API manually you can use the webNotification.requestPermission function.<br>
100+
This function triggers the request permissions dialog in case permissions were not already granted.
101+
102+
```js
103+
//manually ask for notification permissions (invoked automatically if needed and allowRequest=true)
104+
webNotification.requestPermission(function onRequest(granted) {
105+
if (granted) {
106+
console.log('Permission Granted.');
107+
} else {
108+
console.log('Permission Not Granted.');
109+
}
110+
});
111+
```
98112

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

@@ -129,7 +143,7 @@ See [contributing guide](.github/CONTRIBUTING.md)
129143

130144
| Date | Version | Description |
131145
| ----------- | ------- | ----------- |
132-
| 2018-06-14 | v1.0.27 | Maintenance |
146+
| 2018-06-25 | v1.0.28 | Expose webNotification.requestPermission #5 |
133147
| 2018-06-14 | v1.0.26 | Better error detection on chrome mobile #4 |
134148
| 2017-08-25 | v1.0.21 | Support service worker web notifications |
135149
| 2017-01-31 | v1.0.3 | Removed polyfill dependency |

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-web-notification",
3-
"version": "1.0.27",
3+
"version": "1.0.28",
44
"description": "Web Notifications made easy.",
55
"authors": [
66
"Sagie Gur-Ari <sagiegurari@gmail.com>"

docs/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
| Date | Version | Description |
22
| ----------- | ------- | ----------- |
3-
| 2018-06-14 | v1.0.27 | Maintenance |
3+
| 2018-06-25 | v1.0.28 | Expose webNotification.requestPermission #5 |
44
| 2018-06-14 | v1.0.26 | Better error detection on chrome mobile #4 |
55
| 2017-08-25 | v1.0.21 | Support service worker web notifications |
66
| 2017-01-31 | v1.0.3 | Removed polyfill dependency |

docs/api.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<dt><a href="#ShowNotificationCallback">ShowNotificationCallback</a> : <code>function</code></dt>
1313
<dd><p>&#39;showNotification&#39; callback.</p>
1414
</dd>
15+
<dt><a href="#PermissionsRequestCallback">PermissionsRequestCallback</a> : <code>function</code></dt>
16+
<dd><p>&#39;requestPermission&#39; callback.</p>
17+
</dd>
1518
</dl>
1619

1720
<a name="webNotification"></a>
@@ -25,6 +28,7 @@ A simplified web notification API.
2528
* [webNotification](#webNotification) : <code>object</code>
2629
* [.allowRequest](#webNotification.allowRequest) : <code>Boolean</code>
2730
* [.permissionGranted](#webNotification.permissionGranted)
31+
* [.requestPermission([callback])](#webNotification.requestPermission)
2832
* [.showNotification([title], [options], [callback])](#webNotification.showNotification)
2933

3034
<a name="webNotification.allowRequest"></a>
@@ -39,6 +43,28 @@ True to enable automatic requesting of permissions if needed.
3943
True if permission is granted, else false.
4044

4145
**Access**: public
46+
<a name="webNotification.requestPermission"></a>
47+
48+
### webNotification.requestPermission([callback])
49+
Triggers the request permissions dialog in case permissions were not already granted.
50+
51+
**Access**: public
52+
53+
| Param | Type | Description |
54+
| --- | --- | --- |
55+
| [callback] | [<code>PermissionsRequestCallback</code>](#PermissionsRequestCallback) | Called with the permissions result (true enabled, false disabled) |
56+
57+
**Example**
58+
```js
59+
//manually ask for notification permissions (invoked automatically if needed and allowRequest=true)
60+
webNotification.requestPermission(function onRequest(granted) {
61+
if (granted) {
62+
console.log('Permission Granted.');
63+
} else {
64+
console.log('Permission Not Granted.');
65+
}
66+
});
67+
```
4268
<a name="webNotification.showNotification"></a>
4369

4470
### webNotification.showNotification([title], [options], [callback])
@@ -128,3 +154,15 @@ navigator.serviceWorker.register('service-worker.js').then(function(registration
128154
| [error] | <code>error</code> | The error object in case of any error |
129155
| [hide] | <code>function</code> | The hide notification function |
130156

157+
<a name="PermissionsRequestCallback"></a>
158+
159+
## PermissionsRequestCallback : <code>function</code>
160+
'requestPermission' callback.
161+
162+
**Kind**: global typedef
163+
164+
| Param | Type | Description |
165+
| --- | --- | --- |
166+
| [error] | <code>error</code> | The error object in case of any error |
167+
| [hide] | <code>function</code> | The hide notification function |
168+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-web-notification",
3-
"version": "1.0.27",
3+
"version": "1.0.28",
44
"description": "Web Notifications made easy.",
55
"author": {
66
"name": "Sagie Gur-Ari",

project/config/README-template.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $('.some-button').on('click', function onClick() {
6262

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

65-
````js
65+
```js
6666
navigator.serviceWorker.register('service-worker.js').then(function(registration) {
6767
$('.some-button').on('click', function onClick() {
6868
webNotification.showNotification('Example Notification', {
@@ -94,7 +94,21 @@ navigator.serviceWorker.register('service-worker.js').then(function(registration
9494
});
9595
});
9696
});
97-
````
97+
```
98+
99+
In case you wish to invoke the permissions API manually you can use the webNotification.requestPermission function.<br>
100+
This function triggers the request permissions dialog in case permissions were not already granted.
101+
102+
```js
103+
//manually ask for notification permissions (invoked automatically if needed and allowRequest=true)
104+
webNotification.requestPermission(function onRequest(granted) {
105+
if (granted) {
106+
console.log('Permission Granted.');
107+
} else {
108+
console.log('Permission Not Granted.');
109+
}
110+
});
111+
```
98112

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

test/helpers/notify-mock.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
window.Notification = (function Notification() {
22
'use strict';
33

4+
var noop = function () {
5+
return undefined;
6+
};
7+
48
var permissionInfo = {
59
value: null
610
};
711

812
var oncePermission;
913

1014
var Lib = function (title, options) {
11-
Lib.validateNotification(title, options);
15+
var validateNotification = Lib.validateNotification || noop;
16+
validateNotification(title, options);
1217

1318
var self = this;
1419
self.close = function () {
@@ -28,6 +33,12 @@ window.Notification = (function Notification() {
2833
});
2934

3035
Lib.requestPermission = function (callback) {
36+
if (Lib.errorOnPermission) {
37+
setTimeout(function () {
38+
callback(new Error('test'));
39+
}, 5);
40+
}
41+
3142
if (oncePermission) {
3243
oncePermission();
3344
oncePermission = null;
@@ -37,9 +48,7 @@ window.Notification = (function Notification() {
3748
};
3849

3950
Lib.setValidationNotification = function (validateNotification) {
40-
Lib.validateNotification = validateNotification || function noop() {
41-
return undefined;
42-
};
51+
Lib.validateNotification = validateNotification || noop;
4352
};
4453

4554
Lib.setAllowed = function (validateNotification) {

test/spec/web-notification-spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,58 @@ describe('simple-web-notification', function () {
8484
});
8585
});
8686

87+
describe('requestPermission', function () {
88+
it('no callback', function () {
89+
assert.isTrue(window.webNotification.lib.MOCK_NOTIFY);
90+
window.Notification.errorOnPermission = true;
91+
92+
window.webNotification.requestPermission();
93+
94+
window.Notification.errorOnPermission = false;
95+
});
96+
97+
it('callback not a function', function () {
98+
assert.isTrue(window.webNotification.lib.MOCK_NOTIFY);
99+
window.Notification.errorOnPermission = true;
100+
101+
window.webNotification.requestPermission(true);
102+
103+
window.Notification.errorOnPermission = false;
104+
});
105+
106+
it('allowed', function (done) {
107+
assert.isTrue(window.webNotification.lib.MOCK_NOTIFY);
108+
window.Notification.setAllowed();
109+
110+
window.webNotification.requestPermission(function (granted) {
111+
assert.isTrue(granted);
112+
done();
113+
});
114+
});
115+
116+
it('not allowed', function (done) {
117+
assert.isTrue(window.webNotification.lib.MOCK_NOTIFY);
118+
window.Notification.setNotAllowed();
119+
120+
window.webNotification.requestPermission(function (granted) {
121+
assert.isFalse(granted);
122+
done();
123+
});
124+
});
125+
126+
it('error', function (done) {
127+
assert.isTrue(window.webNotification.lib.MOCK_NOTIFY);
128+
window.Notification.errorOnPermission = true;
129+
130+
window.webNotification.requestPermission(function (granted) {
131+
assert.isFalse(granted);
132+
done();
133+
});
134+
135+
window.Notification.errorOnPermission = false;
136+
});
137+
});
138+
87139
describe('showNotification', function () {
88140
describe('allowed', function () {
89141
it('all info', function (done) {

web-notification.js

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
* @param {function} [hide] - The hide notification function
99
*/
1010

11+
/**
12+
* 'requestPermission' callback.
13+
*
14+
* @callback PermissionsRequestCallback
15+
* @param {error} [error] - The error object in case of any error
16+
* @param {function} [hide] - The hide notification function
17+
*/
18+
1119
/**
1220
* A simplified web notification API.
1321
*
@@ -265,6 +273,38 @@
265273
};
266274
};
267275

276+
/**
277+
* Triggers the request permissions dialog in case permissions were not already granted.
278+
*
279+
* @function
280+
* @memberof! webNotification
281+
* @alias webNotification.requestPermission
282+
* @public
283+
* @param {PermissionsRequestCallback} [callback] - Called with the permissions result (true enabled, false disabled)
284+
* @example
285+
* ```js
286+
* //manually ask for notification permissions (invoked automatically if needed and allowRequest=true)
287+
* webNotification.requestPermission(function onRequest(granted) {
288+
* if (granted) {
289+
* console.log('Permission Granted.');
290+
* } else {
291+
* console.log('Permission Not Granted.');
292+
* }
293+
* });
294+
* ```
295+
*/
296+
webNotification.requestPermission = function (callback) {
297+
if (callback && typeof callback === 'function') {
298+
if (isEnabled()) {
299+
callback(true);
300+
} else {
301+
NotificationAPI.requestPermission(function onRequestDone() {
302+
callback(isEnabled());
303+
});
304+
}
305+
}
306+
};
307+
268308
/**
269309
* Shows the notification based on the provided input.<br>
270310
* The callback invoked will get an error object (in case of an error, null in
@@ -352,19 +392,13 @@
352392
var title = data.title;
353393
var options = data.options;
354394

355-
if (isEnabled()) {
356-
createAndDisplayNotification(title, options, callback);
357-
} else if (webNotification.allowRequest) {
358-
NotificationAPI.requestPermission(function onRequestDone() {
359-
if (isEnabled()) {
360-
createAndDisplayNotification(title, options, callback);
361-
} else {
362-
callback(new Error('Notifications are not enabled.'), null);
363-
}
364-
});
365-
} else {
366-
callback(new Error('Notifications are not enabled.'), null);
367-
}
395+
webNotification.requestPermission(function onRequestDone(granted) {
396+
if (granted) {
397+
createAndDisplayNotification(title, options, callback);
398+
} else {
399+
callback(new Error('Notifications are not enabled.'), null);
400+
}
401+
});
368402
}
369403
};
370404

0 commit comments

Comments
 (0)