Push notifications have historically required the user to be online at the moment the message is sent. Scheduled push notifications change that: a notification like "Take your pill" or "You've got a flight in 3 hours" can be shown even if the device has gone offline, because the scheduling happens on the user's device rather than on a server.
Delivering these relies on the Notifications API together with the Push API and the HTTP Web Push Protocol. The scheduling piece comes from the Notification Trigger API, which Chrome 80 began an origin trial for; it lets notifications be triggered by events other than a server push. Only time-based triggers are supported today, though further trigger types such as geolocation are planned. Browser vendors are also working to tame the permission prompts that sites show on load, with Firefox and Chrome both having outlined plans in that direction.
JavaScript timers can schedule work easily, but an app is not guaranteed to be running at the moment a notification should appear, so the logic cannot live at the application layer. It moves into a Service Worker instead.
Constraints before you start
The API is in an early feedback phase. To try it, either enable the #enable-experimental-web-platform-features flag in Chrome or register the application for an origin trial. Since this is Service Worker territory, the page must also be served over HTTPS.
A minimal setup is enough to follow along: an application.js, an index.html, a service-worker.js, and a few image assets. A complete example exists as the Notification Trigger API demo on GitHub.
Registering the worker
Registration comes first; for a first pass the worker only needs to log a successful registration.
// service-worker.js
// listen to the install event
self.addEventListener('install', event => console.log('ServiceWorker installed'));
<!-- index.html -->
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js');
}
</script>
Scheduling and cancelling notifications
At the application layer, ask for notification permission, get the Service Worker registration for the scope, and call showNotification as usual. The new part is the showTrigger property, which defines the conditions for display. Supplying a TimestampTrigger with a timestamp is all it takes, and because the work happens on the device the notification still fires offline.
// application.js
document.querySelector('#notification-button').onclick = async () => {
const reg = await navigator.serviceWorker.getRegistration();
Notification.requestPermission().then(permission => {
if (permission !== 'granted') {
alert('you need to allow push notifications');
} else {
const timestamp = new Date().getTime() + 5 * 1000; // now plus 5000ms
reg.showNotification(
'Demo Push Notification',
{
tag: timestamp, // a unique ID
body: 'Hello World', // content of the push notification
showTrigger: new TimestampTrigger(timestamp), // set the time for the push notification
data: {
url: window.location.href, // pass the current url to the notification
},
badge: './assets/badge.png',
icon: './assets/icon.png',
}
);
}
});
};
Pending notifications can also be withdrawn. Retrieve the notifications still waiting in the Service Worker and close them before they reach the device.
// application.js
document.querySelector('#notification-cancel').onclick = async () => {
const reg = await navigator.serviceWorker.getRegistration();
const notifications = await reg.getNotifications({
includeTriggered: true
});
notifications.forEach(notification => notification.close());
alert(`${notifications.length} notification(s) cancelled`);
};
Reacting to user interaction
Interaction is handled through the Service Worker's notificationclick and notificationclose events, which carry the full capabilities of the worker. A simple handler might open a new window.
// service-worker.js
self.addEventListener('notificationclick', event => {
event.waitUntil(self.clients.openWindow('/'));
});
A more careful version checks whether the required window is already open and only creates one when it is not.
// service-worker.js
self.addEventListener('notificationclick', event => {
event.waitUntil(self.clients.matchAll().then(clients => {
if (clients.length){ // check if at least one tab is already open
clients[0].focus();
} else {
self.clients.openWindow('/');
}
}));
});
Action buttons
Predefined actions give users a faster path — for example, letting them dismiss the notification or open the app instead.
// application.js
reg.showNotification(
'Demo Push Notification',
{
tag: timestamp, // a unique ID
body: 'Hello World', // content of the push notification
showTrigger: new TimestampTrigger(timestamp), // set the time for the push notification
data: {
url: window.location.href, // pass the current url to the notification
},
badge: './assets/badge.png',
icon: './assets/icon.png',
actions: [
{
action: 'open',
title: 'Open app’
},
{
action: 'close',
title: 'Close notification',
}
]
}
);
The Service Worker then consumes those actions.
// service-worker.js
self.addEventListener('notificationclick', event => {
if (event.action === 'close') {
event.notification.close();
} else {
self.clients.openWindow('/');
}
});
Messaging between app and worker
The last piece is communication in the other direction, from the worker to the app, via postMessage on the Service Worker clients. A typical case: the worker tells the tab that is already active that a notification was clicked.
// service-worker.js
self.addEventListener('notificationclick', event => {
event.waitUntil(self.clients.matchAll().then(clients => {
if(clients.length){ // check if at least one tab is already open
clients[0].focus();
clients[0].postMessage('Push notification clicked!');
} else {
self.clients.openWindow('/');
}
}));
});
// application.js
navigator.serviceWorker.addEventListener('message', event => console.log(event.data));
Where things stand
Scheduled delivery is a meaningful improvement to the Notification API and to the mobile experience of web apps, and the Notification Trigger API is still under active development — which makes this a good moment to experiment with it and send feedback to the developers. For Vue or React projects, a Progressive Web App demo is available with a documented Notification Trigger API example for both frameworks.




