Skip to content

Commit

Permalink
Update firebase-messaging-sw.js
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov authored Feb 13, 2017
1 parent 219d36e commit 20fa13f
Showing 1 changed file with 1 addition and 42 deletions.
43 changes: 1 addition & 42 deletions firebase-messaging-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,4 @@ firebase.initializeApp({
'messagingSenderId': '448358493027'
});

const messaging = firebase.messaging();

// Customize notification handler
messaging.setBackgroundMessageHandler(function(payload) {
if (typeof payload.data.time != 'undefined') {
var time = new Date(payload.data.time * 1000);
var now = new Date();
if (time < now) { // expired
return null;
}
var diff = Math.round((time.getTime() - now.getTime()) / 1000);

payload.data.body = 'Начало через ' + Math.round(diff / 60) + ' минут, в ' + time.getHours() + ':' + time.getMinutes();
}

payload.data.data = payload.data;

return self.registration.showNotification(payload.data.title, payload.data);
});

self.addEventListener('notificationclick', function(event) {
console.log(event);
var target = event.notification.data.click_action || '/';
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) {
console.log(clientList);
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == target && 'focus' in client) {
return client.focus();
}
}

if (clients.openWindow) {
return clients.openWindow(target);
}
}));
});
firebase.messaging();

0 comments on commit 20fa13f

Please sign in to comment.