Maxim Salnikov
@webmaxru
My App
Azure Developer Technical Lead at Microsoft
if (!('serviceWorker' in navigator)) {
// Service Worker isn't supported on this browser
// Disable or hide UI.
return;
}
if (!('PushManager' in window)) {
// Push isn't supported on this browser
// Disable or hide UI.
return;
}
Notification.requestPermission().then(function(result) {
if (result === 'denied') {
console.log('Permission wasn\'t granted');
return;
}
if (result === 'default') {
console.log('The permission request was dismissed');
return;
}
// Do something with the granted permission
// result === 'granted' in this case
});
Allow users to unsubscribe.
Otherwise they'll block!
navigator.serviceWorker.register('/service-worker.js')
.then(function(registration) {
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: 'key...'
};
return registration.pushManager.subscribe(subscribeOptions);
})
.then(function(pushSubscription) {
console.log(JSON.stringify(pushSubscription));
return pushSubscription;
});
{
"endpoint": "https://particular-push-service.com/long-unique-id/",
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA=",
"auth": "tBHItJI5svbpez7KI4CCXg=="
}
}
self.addEventListener('push', function(event) {
notification = event.data.json()
event.waitUntil(
self.registration.showNotification(
notification.title, notification.options
)
)
})
const title = 'Something Has Happened'
const options = {
message: 'Something you might want to check out',
icon: '/assets/images/logo.png'
}
self.registration.showNotification(title, options)
Online check in is available
MyAirline
myairline.com
Flight
DY1043
Depart
21.09 13:45
Click here to check in now
myairline.com
self.addEventListener('notificationclose', function(event) {
const dismissedNotification = event.notification
event.waitUntil( triggerAnalytics( dismissedNotification ) )
})
self.addEventListener('notificationclick', function(event) {
const clickedNotification = event.notification
clickedNotification.close()
event.waitUntil( performAction( clickedNotification ) );
});
Maxim Salnikov
@webmaxru
Maxim Salnikov
@webmaxru