Skip to content

Web Push

The page half of web push. It checks whether a page can get notifications, asks the person to allow them, subscribes the plugin's own service worker with the server's VAPID public key, and sends the subscription to the plugin. See Notifications for the server half and the service worker.

Web push needs HTTPS

Browsers allow push only on a secure origin, a page opened over HTTPS, with localhost the one exception. On an iPhone or iPad, the page must be added to the home screen first. The person must allow notifications.

Decision

When Use
A page offers notifications on this device subscribeWebPush from a button's click
A page shows whether this device gets them webPushState and its message
A message in the dashboard while a device is online The Jellyfin channel of StolonNotifier, with no page code
A page that loads neither the bundle nor its CSS The standalone page script, whose helpers resolve to the state alone

Usage

import { webPushState, subscribeWebPush, unsubscribeWebPush } from './configurationpage?name=myplugin_stolon.js';

var registration = navigator.serviceWorker.register('sw.js');
webPushState(registration).then(function (r) { status.textContent = r.message; });

button.addEventListener('click', function () {
    shared.apiRequest('Push/Key', 'GET').then(function (key) {
        return subscribeWebPush({
            registration: registration,
            publicKey: key.PublicKey,
            save: function (subscription) { return shared.apiRequest('Push/Subscribe', 'POST', subscription); }
        });
    }).then(function (r) { status.textContent = r.message; });
});

States and Modifiers

State When Message
insecure window.isSecureContext is false "Notifications work only when the page opens over HTTPS."
install An iPhone or iPad page that is not running from the home screen "Add this page to the home screen to get notifications on an iPhone or iPad."
unsupported No service worker, PushManager, or Notification "This browser cannot show notifications."
denied The person blocked notifications "Notifications are blocked. Allow them in the browser's settings for this site."
off Push works and there is no subscription "Notifications are off."
on The service worker holds a subscription and notifications are allowed "Notifications are on."
  • subscribeWebPush asks for permission, reuses a subscription made with the same key, replaces one made with another, then calls save with { endpoint, keys: { p256dh, auth }, label }. It resolves to on, or to denied or off when the person does not allow it.
  • unsubscribeWebPush calls remove with the subscription, then removes it from the browser, and resolves to off.
  • webPushText(state) gives a state's message in the page's language, and webPushLabel() a short name for the device, such as "Safari · iPhone", which the subscription carries as its label.

Gotchas

  • Call subscribeWebPush from a click. Browsers ask for permission only after a gesture, and Safari refuses the ask otherwise.
  • The registration is the plugin's own service worker, and its script must show the push. Load Stolon's worker script into it. See The Service Worker.
  • publicKey is required, and a missing one rejects.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

webPushState

function webPushState(registration?: ServiceWorkerRegistration | PromiseLike<ServiceWorkerRegistration> | null): Promise<WebPushResult>

Says where a page stands with web push, without asking the person anything. Push works only on a secure origin, HTTPS or localhost, and an iPhone or iPad page only once it is added to the home screen.

subscribeWebPush

function subscribeWebPush(options: WebPushOptions): Promise<WebPushResult>

Asks the person to allow notifications, subscribes the plugin's service worker with the server's VAPID public key, and sends the subscription to the plugin through save. A subscription made with another key is replaced. Call it from a click, since browsers ask only after a gesture.

Option Type What it does
registration (required) ServiceWorkerRegistration | PromiseLike<ServiceWorkerRegistration> the plugin's service worker registration, or a promise of it, such as navigator.serviceWorker.register('sw.js')
publicKey string the server's VAPID public key, base64url, from StolonNotifier.GetPublicKey
save (subscription: WebPushSubscriptionJson): PromiseLike<unknown> function(subscription) that sends { endpoint, keys, label } to the plugin and returns a promise [optional]
remove (subscription: WebPushSubscriptionJson): PromiseLike<unknown> not used to subscribe [optional]

unsubscribeWebPush

function unsubscribeWebPush(options: WebPushOptions): Promise<WebPushResult>

Removes the plugin's subscription from the plugin through remove, then from the browser.

Option Type What it does
registration (required) ServiceWorkerRegistration | PromiseLike<ServiceWorkerRegistration> the plugin's service worker registration, or a promise of it
publicKey string not used to unsubscribe [optional]
save (subscription: WebPushSubscriptionJson): PromiseLike<unknown> not used to unsubscribe [optional]
remove (subscription: WebPushSubscriptionJson): PromiseLike<unknown> function(subscription) that tells the plugin to forget it and returns a promise [optional]

webPushText

function webPushText(state: WebPushState): string

The sentence a page shows for a web push state, in the page's language.

webPushLabel

function webPushLabel(userAgent?: string): string

A short name for the device a page runs on, such as "Safari · iPhone", for the plugin's list of subscriptions.

Type What it is
WebPushState Where a page stands with web push. unsupported has no push in the browser, insecure is a page not on HTTPS, install is an iPhone or iPad page not yet added to the home screen, denied is a person who blocked notifications, off has none yet, and on is subscribed.
WebPushSubscriptionJson The subscription a page sends its plugin, as PushSubscription.toJSON gives it, with a short name for the device.
WebPushResult What subscribeWebPush, unsubscribeWebPush, and webPushState resolve to: the state, and its sentence for the person.

Example

Example Code

<!-- title: Web Push -->
<div id="out"></div>
<script type="module">
const base = await window.__baseReady;
const list = base.createDefinitionList({ items: [] });
document.getElementById('out').appendChild(list.element);
const states = ['insecure', 'install', 'unsupported', 'denied', 'off', 'on'];
base.webPushState().then((r) => {
    list.setItems([{ label: 'This page', value: r.state + ', ' + r.message }]
        .concat({ label: 'This device', value: base.webPushLabel() || 'Unknown' })
        .concat(states.map((s) => ({ label: s, value: base.webPushText(s) }))));
});
</script>