Skip to content

Commit b37be48

Browse files
committed
Some work on README
1 parent 2627cc8 commit b37be48

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ Enable browser push notifications using Angular
44
## Install
55

66
```
7-
bower install angular-browser-push-notifications
7+
$ bower install angular-browser-push-notifications
88
```
99

1010
## Pre-requisites
1111

12-
- You must run this on a domain verified in [Google developer console](https://console.developers.google.com)
13-
**Important** It must be verified either using the domain registrar method or must be https
12+
- If you are not running on `localhost`, you must run this on a HTTPS domain verified in [Google developer console](https://console.developers.google.com)
1413
- If you are using https, you must ensure that the certificate is fully trusted
1514
- You must have a `manifest.json` file referenced in your HTML as follows
1615
``` <link rel="manifest" href="manifest.json"> ```
16+
- You need a service worker available *at the root* of your site. See [examples](./examples) for sample manifest and service workers
17+
- Refer to [this article](https://developers.google.com/web/updates/2015/03/push-notificatons-on-the-open-web) for more information on Chrome push notifications
1718

1819
## How to use
1920

@@ -53,6 +54,12 @@ Possible reasons for failure:
5354
- WORKER_REGISTRATION_FAIL: Worker registration failed, usually related to invalid certificate
5455
- FAILED_TO_SUBSCRIBE: Subscription failed, could be related to certificate or to your application key
5556

57+
## Configuration methods
58+
59+
### BrowserPushNotificationsProvider.setWorkerUrl
60+
61+
Set the url for the service worker which will be registered
62+
5663
## Sample code
5764

5865
```js
@@ -69,9 +76,7 @@ angular.module('notificationsApp', ['browserPushNotifications'])
6976

7077
## Demo
7178

72-
[Demo site](https://anacondapp.cloudapp.net)
73-
74-
*Note* that you need to **trust** the certificate for this demo to work.
79+
[Demo site](https://www.redapesolutions.com/pushnotifications)
7580

7681

7782

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"test",
99
"README.md",
1010
"README.html",
11-
"examples"
11+
"examples",
12+
"Gruntfile.coffee"
1213
]
1314
}
1415

examples/service-worker-with-fetch.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
self.addEventListener('push', function(event) {
4+
console.log('Received a push message', event);
5+
var title = 'Anacondapp';
6+
var icon = '/images/icon-192x192.png';
7+
var tag = 'anacondapp'+Date.now();
8+
9+
var notificationPromise = event.currentTarget.registration.pushManager.getSubscription().then(function(sub) {
10+
var id = sub.endpoint.split('/').pop();
11+
return fetch('/communications/details?token='+id);
12+
}).then(function(response){
13+
return response.text();
14+
}).then(function(body) {
15+
return self.registration.showNotification(title, {
16+
body: body,
17+
icon: icon,
18+
tag: tag
19+
});
20+
});
21+
22+
event.waitUntil(notificationPromise);
23+
24+
// Close after 5 seconds if still there - more browser like than mobile phone
25+
notificationPromise.then(function() {
26+
return new Promise(function(resolve){
27+
setTimeout(resolve, 5000)
28+
});
29+
}).then(function() {
30+
return event.currentTarget.registration.getNotifications();
31+
}).then(function(notifications) {
32+
for(var i=0;i<notifications.length;i++){
33+
var notification = notifications[i];
34+
if(notification.tag === tag){
35+
notification.close();
36+
break;
37+
}
38+
}
39+
});
40+
});
41+
42+
43+
self.addEventListener('notificationclick', function(event) {
44+
console.log('On notification click: ', event);
45+
// Android doesn’t close the notification when you click on it
46+
// See: http://crbug.com/463146
47+
event.notification.close();
48+
49+
// This looks to see if the current is already open and
50+
// focuses if it is
51+
event.waitUntil(clients.matchAll({
52+
type: "window"
53+
}).then(function(clientList) {
54+
for (var i = 0; i < clientList.length; i++) {
55+
var client = clientList[i];
56+
if (client.url == '/' && 'focus' in client)
57+
return client.focus();
58+
}
59+
if (clients.openWindow)
60+
return clients.openWindow('/');
61+
}));
62+
63+
});

0 commit comments

Comments
 (0)