-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsw.js
79 lines (71 loc) · 2.76 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var NOTIFICATION_ENDPOINT = "/example-fetch.json";
var URL_TO_DEFAULT_ICON = "/images/icon-192x192.png";
self.addEventListener('push', function(event) {
console.log('Received a push message', event);
// Since there is no payload data with the first version
// of push messages, we'll grab some data from
// an API and use it to populate a notification
event.waitUntil(
fetch(NOTIFICATION_ENDPOINT).then(function(response) {
if (response.status !== 200) {
// Either show a message to the user explaining the error
// or enter a generic message and handle the
// onnotificationclick event to direct the user to a web page
console.log('Looks like there was a problem. Status Code: ' + response.status);
throw new Error();
}
// Examine the text in the response
return response.json().then(function(data) {
if (data.error || !data.notification) {
console.error('The API returned an error.', data.error);
throw new Error();
}
var title = data.notification.title;
var message = data.notification.message;
var icon = data.notification.icon;
var notificationTag = data.notification.tag;
var notificationData = data.notification.data;
return self.registration.showNotification(title, {
body: message,
icon: icon,
tag: notificationTag,
data: notificationData
});
});
}).catch(function(err) {
console.error('Unable to retrieve data', err);
var title = 'An error occurred';
var message = 'We were unable to get the information for this push message';
var icon = URL_TO_DEFAULT_ICON;
var notificationTag = 'simple-push-demo-notification-tag';
return self.registration.showNotification(title, {
body: message,
icon: icon,
tag: notificationTag
});
})
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event);
// check for data support
if (Notification.prototype.hasOwnProperty('data')) {
console.log('Using Data');
var url = event.notification.data.url;
event.waitUntil(clients.openWindow(url));
} else {
console.error('Could not get notification data. Fetching notification from server instead.');
var request = fetch(NOTIFICATION_ENDPOINT)
.then(function(res) {
return res.json();
});
event.waitUntil(request)
.then(function(res) {
// At the moment you cannot open third party URL's, a simple trick
// is to redirect to the desired URL from a URL on your domain
var redirectUrl = '/?redirect=' +
res.notification.data.url;
return clients.openWindow(redirectUrl);
});
}
});