Maxim Salnikov
@webmaxru
What's available in browser's Background Services
Developer Audience Lead at Microsoft
Service
worker
OS
Browser
Internet
App
Event
Event
Event
Event
Message
(Triggers)
const registration = await navigator.serviceWorker.ready;
if ('periodicSync' in registration) {
registration.periodicSync.register('update-content', {
// An interval of one day.
minInterval: 24 * 60 * 60 * 1000
})
}
main.js
self.addEventListener('periodicsync', (event) => {
if (event.tag === 'update-content') {
// Think before you sync!
event.waitUntil(updateContent());
}
});
service-worker.js
Engagement Score | Periodic Sync Interval (hours) |
---|---|
NONE | Never |
MINIMAL | 36 |
LOW | 24 |
MEDIUM | 24 |
HIGH | 12 |
MAX | 12 |
const registration = await navigator.serviceWorker.ready;
if ('showTrigger' in Notification.prototype) {
// In 5 seconds from now
const triggerTime = Date.now() + 5 * 1000
registration.showNotification('Notification Trigger Demo', {
body: `Scheduled for ${new Date( triggerTime ).toLocaleTimeString()}`,
showTrigger: new TimestampTrigger( triggerTime )
});
}
main.js
const registration = await navigator.serviceWorker.ready;
if ('showTrigger' in Notification.prototype) {
registration.showNotification('Reminder', {
tag: 'update-content',
body: "Click to sync with the server",
// Tomorrow, same time
showTrigger: new TimestampTrigger(Date.now() + 24 * 60 * 60 * 1000)
});
}
main.js
self.addEventListener('notificationclick', (event) => {
if (event.notification.tag === 'update-content') {
event.waitUntil(updateContentAndLaunchApp());
}
});
service-worker.js
Long answer
Short answer
* Progressive = "Progressive Enhancement"
Maxim Salnikov
@webmaxru