service-worker.js 1.36 KB
'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));
});