-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
106 lines (99 loc) · 4.04 KB
/
service-worker.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
self.addEventListener('push', function(event) {
if (!event.data) return;
var notification = event.data.json();
event.waitUntil(
self.registration.showNotification(notification.title, notification.options)
);
});
self.addEventListener('push', function(event) {
if (event.data) return;
event.waitUntil(
self.registration.pushManager.getSubscription().then(function(subscription) {
var notificationsPath = 'https://pushpad.xyz/notifications?endpoint=' + encodeURIComponent(subscription.endpoint);
if (typeof subscription.toJSON === 'function') {
notificationsPath += '&p256dh=' + subscription.toJSON().keys.p256dh;
notificationsPath += '&auth=' + subscription.toJSON().keys.auth;
}
var headers = new Headers();
headers.append('Accept', 'application/json');
return fetch(notificationsPath, {headers: headers});
}).then(function(response) {
if (response.status !== 200) {
throw new Error('The API returned an error. Status Code: ' + response.status);
}
return response.json();
}).then(function(notifications) {
return Promise.all(
notifications.map(function (notification) {
var notificationOptions = {
body: notification.body,
tag: notification.id,
requireInteraction: notification.require_interaction,
data: { custom: notification.custom_data }
};
if (notification.icon_url) {
notificationOptions.icon = notification.icon_url;
}
if (notification.image_url) {
notificationOptions.image = notification.image_url;
}
if (notification.actions && notification.actions.length) {
notificationOptions.actions = [];
for (var actionIndex = 0; actionIndex < notification.actions.length; actionIndex++) {
notificationOptions.actions[actionIndex] = {
action: notification.actions[actionIndex].action,
title: notification.actions[actionIndex].title
};
if (notification.actions[actionIndex].icon) {
notificationOptions.actions[actionIndex].icon = notification.actions[actionIndex].icon;
}
}
}
return self.registration.showNotification(notification.title, notificationOptions);
})
);
}).catch(function(err) {
console.error('An error occurred while processing the push event.', err);
})
);
});
self.addEventListener('notificationclick', function(event) {
// Android doesn't close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
var targetUrl = 'https://pushpad.xyz/notifications/' + event.notification.tag + '/redirect';
if (event.action) {
targetUrl += '?notification_action=' + event.action;
}
// if custom action
if (event.action && self.notificationActions && self.notificationActions.hasOwnProperty(event.action)) {
event.waitUntil(
Promise.all([
fetch(targetUrl, { headers: new Headers({'Accept': 'application/json'}) }),
self.notificationActions[event.action](event.notification.data.custom)
.catch(function(err) {
console.log('A custom action has been invoked but it has raised an exception: ' + err);
})
])
);
// else open target url
} else {
event.waitUntil(
clients.openWindow(targetUrl)
);
}
});
self.addEventListener('pushsubscriptionchange', function(event) {
event.waitUntil(
fetch('https://pushpad.xyz/pushsubscriptionchange', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
old_endpoint: event.oldSubscription ? event.oldSubscription.endpoint : null,
new_endpoint: event.newSubscription ? event.newSubscription.endpoint : null,
new_p256dh: event.newSubscription ? event.newSubscription.toJSON().keys.p256dh : null,
new_auth: event.newSubscription ? event.newSubscription.toJSON().keys.auth : null
})
})
);
});