Agent Wranglers

NTF-17Recommended

Show a notification for every push

Every web push the service worker receives shows a notification before the handler ends. Never use push to sync data in the background without showing anything.

How to do it

Subscribe with userVisibleOnly: true, which the Push API uses to promise that every push is made visible. Safari does not support invisible pushes, and it revokes the site's permission when a push shows nothing. On Android, FCM may lower the priority of high-priority messages that show no notification. So call showNotification() inside event.waitUntil(), even when the payload is empty or the item is already read. Then show a short line such as "You have new activity". Fetch other data when the app opens instead. The full text lives in the server-side inbox.

Examples

JavaScript example

self.addEventListener('push', event => {
  const message = readPayload(event.data);
  event.waitUntil(self.registration.showNotification(message.title ?? 'You have new activity', {
    body: message.body ?? '',
    tag: message.topic,
    data: { path: message.path ?? '/notifications' },
  }));
});

function readPayload(data) {
  try { return data?.json() ?? {}; } catch { return {}; }
}

Python example

def fcm_message(token: str, title: str, body: str, high_priority: bool) -> dict:
    message = {"token": token, "notification": {"title": title, "body": body}}
    if high_priority:
        message["android"] = {"priority": "high"}
    return {"message": message}

def test_high_priority_messages_are_visible():
    for kind in PUSH_TEMPLATES:
        message = render_fcm(kind, SAMPLE_DATA[kind])["message"]
        if message.get("android", {}).get("priority") == "high":
            assert "notification" in message

How to check

A unit test runs the service worker's push handler with a normal payload, an empty payload and a broken payload. Each run calls showNotification once. A server test finds no high-priority FCM message to a web push client without a notification block. Native Android data messages are out of scope here.

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/visible-push-only">Agent Wranglers notifications playbook</a>. In code you write, add a comment next to it with the same link.