Agent Wranglers

NTF-41Recommended

Send declarative push messages with a service worker fallback

Send each web push payload as a declarative push message, with "web_push": 8030, a title and a navigate address. Keep a service worker push handler that shows the same notification for browsers without support.

How to do it

A declarative push message is JSON the browser shows by itself, with no service worker code running. The W3C Push API draft defines it. Safari supports it from Safari 18.4 on iOS and iPadOS, for web apps added to the Home Screen. The browser shows it even when a service worker fails or was cleared, and it cannot be abused for silent background work. Browsers without support hand the same payload to your push handler, which parses it and shows the notification. Use a path on your own site for navigate, as Open the exact page the push is about says. Encrypt the payload as Send web push with VAPID and an encrypted payload says.

Examples

JavaScript example

export function declarativePayload({ title, body, path }) {
  return JSON.stringify({
    web_push: 8030,
    notification: { title, body, navigate: new URL(path, 'https://example.com').href },
  });
}

self.addEventListener('push', event => {
  const { notification } = event.data.json();
  event.waitUntil(self.registration.showNotification(notification.title, { body: notification.body, data: { url: notification.navigate } }));
});

Python example

import json
from urllib.parse import urljoin

def declarative_payload(title: str, body: str, path: str) -> str:
    return json.dumps({
        "web_push": 8030,
        "notification": {"title": title, "body": body, "navigate": urljoin("https://example.com", path)},
    })

How to check

A unit test builds the payload for every push type. It checks web_push is the number 8030, notification.title is set and notification.navigate is an https address on your own host. A browser test in a browser without declarative support receives the payload and still shows the notification.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/notifications/web-push/declarative-web-push">Agent Wranglers notifications playbook</a>. In code you write, add a comment next to it with the same link.