Push Notifications
Make Web applications great again
Notifications
Push Notification flow
Cloud Services
Push Notification. What’s required?
Safary Support
Service Worker
Why I need this?
What Service Worker can
● Push Manager
● Notification
● Cache API
● Proxy
● Background Sync
● Periodic Background Sync (under development)
● Message API
● Tabs Management
Register Service Worker
window.navigator.serviceWorker
.register('/sw.js’ {scope: 'some-scope'})
.then(function (registration) {
//do some stuff
});
Installation flow
MessageEvent From SW
window.navigator.serviceWorker.onmessage = function (event) {};
self.clients
.matchAll({includeUncontrolled: true, type: 'window'})
.then(function(clientList) {
return clientList[0].focus().then(function (windowClient) {
var params = {
action: 'someAction',
data: 'someData'
};
windowClient.postMessage(params);
});
})
MessageEvent To SW
self.addEventListener('message', function(event) {})
registration.active.postMessage({someKey: ‘someValue’});
Cache
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.addAll([
'/gallery/bountyHunters.jpg',
'/gallery/myLittleVader.jpg',
'/gallery/snowTroopers.jpg'
]);
})
);
});
Cache with Proxy
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request).catch(function() {
return fetch(event.request);
}).then(function(response) {
var response = r;
caches.open('v1').then(function(cache) {
cache.put(event.request, response);
});
return response.clone();
}).catch(function() {
return caches.match('/gallery/myLittleVader.jpg');
}));
});
Background Sync
registration.sync.register('someTag');
self.addEventListener('sync', function (event) {
if (event.tag === 'someTag') {
event.waitUntil(doSomeStuff());
}
});
Notification
registration.showNotification('Notification Sample', {
body: 'Buzz!nBuzz!',
icon: '/icon.png',
badge: '/badge.png',
actions: [{action: 'action', title: 'Title', icon: '/icon.png'}],
vibrate: [200, 100, 200],
tag: 'some-id',
data: {}
});
self.addEventListener('notificationclick', function (event) {});
Notification receive
badge
Icon
Title
Description
Action Icon
Firebase Cloud Messages
How to use it?
Push Notification Messages
Message Priority:
● high
● low
● 1-10 (APNs)
Message Type
● Notification
● Data
● Mixed
Send Push to specific device
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=...
{
"to" : "registrationToken",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
Send Push to multiple devices
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=...
{
"registration_ids" : ["registrationToken1","registrationToken2"...],
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
Subscribe to Topic
https://iid.googleapis.com/iid/v1/{registrationToken}/rel/topics/{topicName}
Content-Type:application/json
Content-Length: 0
Authorization:key=...
Send Push to Topic
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=...
{
"to": "/topics/foo-bar",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
Send Push to Multiple Topics
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=...
{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
Recommendations
Let’s Push it!
or at least try

Academy PRO: Push notifications. Denis Beketsky