Skip to content

Instantly share code, notes, and snippets.

@iKrevedko
Created November 14, 2016 13:35
Show Gist options
  • Save iKrevedko/0d5c301fb9d777b647625ae58a9189f0 to your computer and use it in GitHub Desktop.
Save iKrevedko/0d5c301fb9d777b647625ae58a9189f0 to your computer and use it in GitHub Desktop.
'use strict';
self.addEventListener('push', function(event) {
if (!(self.Notification && self.notification.permission === 'granted')) {
return;
}
var data = {};
if (event.data) {
data = event.data.json();
}
var title = data.title || "Новый заказ";
var body = data.message;
var order_id = data.order_id
console.log('Received a push message', event);
event.waitUntil(
self.registration.showNotification(title, {
body: body,
tag: order_id
})
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.title);
// Android doesn’t close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: 'window'
}).then(function(clientList) {
if (clients.openWindow) {
return clients.openWindow('/admin2/orders/' + event.notification.tag);
}
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment