Intro to Web Push and Notifications
Engagement & re-engagement �for the Web
Progressive Web Apps
Progressive Web Apps
Push notification demos
Progressive Web Apps
What are Push Notifications?
Progressive Web Apps
Push Notification overview
Client
Web Page
Service worker
User Agent
Push Service
App Server
Progressive Web Apps
The Notification API
Progressive Web Apps
Request permission
Notification.requestPermission(function(status) {
console.log('Notification permission status:', status);
});
Progressive Web Apps
TODO
Screencast of notification permission being requested.
Progressive Web Apps
Invocation
Client
Web Page
Service worker
User Agent
Push Service
App Server
Progressive Web Apps
Display a notification
function displayNotification() {
if (Notification.permission === 'granted') {
navigator.serviceWorker.getRegistration()
.then(function(reg){
reg.showNotification('Hello world!');
});
}
}
Progressive Web Apps
Add notification options
var options = {
body: 'Here is a notification body!',
icon: 'images/example.png',
vibrate: [100, 50, 100],
data: { primaryKey: 1 } // allows us to identify notification
};
reg.showNotification('Hello world!', options);
Progressive Web Apps
TODO
Screencast of notification display
from example on previous page.
Progressive Web Apps
Add notification actions
var options = {
body: 'First notification!',
actions: [
{action: 'explore', title: 'Go to the site', icon: 'img/check.png'},
{action: 'close', title: 'No thank you', icon: 'img/x.png'},
]
};
reg.showNotification('Hello world!', options);
Progressive Web Apps
Progressive Web Apps
Interaction
Client
Web Page
Service worker
User Agent
Push Service
App Server
Progressive Web Apps
Notification interactions
Two notification events you can listen for in a service worker:
Progressive Web Apps
notificationclose
self.addEventListener('notificationclose', function(event) {
var notification = event.notification;
var primaryKey = notification.data.primaryKey;
console.log('Closed notification: ' + primaryKey);
});
Progressive Web Apps
notificationclick
self.addEventListener('notificationclick', function(event) {
var notification = event.notification;
var action = event.action;
if (action === 'close') {
notification.close();
} else {
clients.openWindow('http://www.example.com');
}
});
Progressive Web Apps
Sending and Receiving Push Notifications
Progressive Web Apps
How it works
Progressive Web Apps
Push API
Client
Web Page
Service worker
User Agent
Push Service
App Server
Progressive Web Apps
Subscribe to the Push service
Client
Web Page
Service worker
User Agent
Push Service
App Server
Progressive Web Apps
Subscribing users
Browser
Server
Check if User is Subscribed
Ask User�To Subscribe
User�Subscribes
Send Subscription
Save Subscription
Progressive Web Apps
Check if user is subscribed
navigator.serviceWorker.ready.then(function(reg) {� reg.pushManager.getSubscription().then(function(sub) {� if (sub == undefined) {� // ask user to register for Push� } else {� // You have subscription, update the database on your server� }� });�});
Progressive Web Apps
Subscribe to the push service
navigator.serviceWorker.getRegistration()
.then(function(reg) {
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
// send sub.toJSON() to server
});
});
Progressive Web Apps
The subscription object
{
"endpoint": "https://fcm.googleapis.com/fcm/send/f1LsxkKp...",
"keys": {
"p256dh": "BLc4xRzKlKORKWlbdgFaB1oEKgPpWC5cW8OCzVrOQRv-1n ...",
"auth": "5I2Bu2oKdyy9CwL8QVF0NQ=="
}
}
Progressive Web Apps
Send a push message from the server
Progressive Web Apps
Sending messages
End Point
Browser
Generate Message
Sent to�End Point
Sent to Browser
Server
Received�by Browser
Progressive Web Apps
Send a message from the server
var webPush = require('web-push');
var payload = 'Here is a payload!';
var options = {
TTL: 60 // max time in seconds for push service to retry delivery
};
webPush.sendNotification(pushSubscription, payload, options);
Progressive Web Apps
What is VAPID?
Progressive Web Apps
Subscribe with the VAPID public key
var applicationServerPublicKey = 'BLiZBfZJTwbWe_TzKaKuiT8GHqmcFU';
var applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
});
Progressive Web Apps
Send a message with VAPID
var webPush = require('web-push');
var payload = 'Here is a payload!';
var options = {
vapidDetails: {
subject: 'mailto: example-email@example.com',
publicKey: vapidPublicKey,
privateKey: vapidPrivateKey
}
};
webPush.sendNotification(pushSubscription, payload, options);
Progressive Web Apps
Receiving messages
Client
Web Page
Service worker
User Agent
Push Service
App Server
Progressive Web Apps
Receiving messages
Browser
SW Starts
Handle�Message
Show Notification
Push Arrives
Progressive Web Apps
The push event
self.addEventListener('push', function(e) {
var title = e.data.text();
e.waitUntil(
self.registration.showNotification(title)
);
});
Progressive Web Apps
Lab Overview
Progressive Web Apps
Be aware
WARNING: Do not use private/incognito mode for this lab.
Push notifications are not supported in private/incognito mode for security reasons.
Progressive Web Apps