service-worker.js
1.36 KB
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
'use strict';
function showNotification(title, body, icon, data) {
var notificationOptions = {
body: body,
icon: icon ? icon : '/app/flashy.ico',
tag: 'card-review-notification',
data: data
};
if (self.registration.showNotification) {
self.registration.showNotification(title, notificationOptions);
return;
} else {
new Notification(title, notificationOptions);
}
}
self.addEventListener('push', function(event) {
console.log('Received a push message', event);
// Since this is no payload data with the first version
// of Push notifications, here we'll grab some data from
// an API and use it to populate a notification
var title = 'You have cards waiting to be reviewed!';
var message = 'check yo cards m8';
var icon = '/app/flashy.ico';
var notificationTag = 'card-review-notification';
// Add this to the data of the notification
var urlToOpen = '/api/subscribe/';
var notificationFilter = {
tag: 'card-review-notification'
};
var notificationData = {
url: urlToOpen
};
return showNotification(title, message, icon, notificationData);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event);
var url = '/app/study';
event.waitUntil(clients.openWindow(url));
});