---
title: "Send web push notifications"
description: "Notifications on a person's phone or desktop with the app closed, in Safari, Chromium and Firefox: VAPID keys env push generates, messages signed and encrypted inside your deployment, usePushSubscription in a click, and each kind opted into."
canonical_url: "https://nuxt-backend.dev/docs/build/email/notifications/web-push"
---
# Send web push notifications

> Notifications on a person's phone or desktop with the app closed, in Safari, Chromium and Firefox: VAPID keys env push generates, messages signed and encrypted inside your deployment, usePushSubscription in a click, and each kind opted into.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- **You get:** notifications on a phone's lock screen or a desktop, with the app closed, in Safari, Chromium and Firefox.
- **Powered by:** the Web Push protocol and the browsers' own push services, including Declarative Web Push in Safari 18.4 and later. No push provider account.
- **The package adds:** the VAPID keys `env push` generates, signing and encryption inside your deployment, `usePushSubscription`, a small service worker, cleanup of dead devices, and quiet hours.

Reach people when the app is closed: a notification on their lock screen that their strip is ready, or that their credits are running low. Web push is a channel of the notifications capability, beside the inbox and email. A person allows it on a device and picks the kinds they want there. The deployment then sends each one to every device they allowed, through the browsers' own push services, with no push provider account and no dependency added.

## How it works

1. **The app's own keys.** A VAPID key pair identifies your app to the browsers' push services. It is yours alone, never derived from `AUTH_SECRET`, so rotating that secret cuts off no device.
2. **A person subscribes a device.** In a click handler, the browser asks for permission and returns the device's push address, which the deployment stores against the account.
3. **The deployment sends.** A notification whose kind allows push is signed and encrypted inside your deployment, in Convex's default runtime with the Web Crypto API. It is posted to each of the person's devices, a few at a time.
4. **The answers keep the list clean.** A device that has gone away (`404` or `410`) is dropped (`notifications.push_device_gone`). A busy push service (`429`, `5xx`) is tried again after the wait it asks for, up to five times (`notifications.push_failed` after the last). A payload too large (`413`) is dropped and logged (`notifications.push_too_large`). A refusal of the keys (`400`, `403`) is logged as an error, with a hint that the key pair does not match (`notifications.push_refused`).

## Create the keys

Run `npx nuxt-backend env push`. When `.env.local` has no key pair, it generates one, writes it there and sets it on the deployment. `env push --prod` does the same for production, with a pair of its own:

<table>
<thead>
  <tr>
    <th>
      Variable
    </th>
    
    <th>
      What it is
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        PUSH_PUBLIC_KEY
      </code>
    </td>
    
    <td>
      The public key. Browsers receive it when they subscribe, so it is public.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        PUSH_PRIVATE_KEY
      </code>
    </td>
    
    <td>
      The private key. It stays on the deployment, never in the app's public config.
    </td>
  </tr>
</tbody>
</table>

Keep each pair for the life of its deployment, and back it up with your other secrets. A browser binds its subscription to the public key, so new keys orphan every subscribed device, and `env push` never replaces an existing pair. The push services also see a contact for the app, which the package derives from `SITE_URL`. Apple's service refuses a `localhost` one, so test Safari against a deployed preview. Until both keys are set, push is off, `status` shows the `notifications` capability as `missing`, and the other channels carry on.

## Turn it on

The scaffold's `backend/notifications.ts` passes the email instance and re-exports the push functions; add them to a file of yours that predates them:

```ts [backend/notifications.ts]
export const notifications = setupNotifications(components, { email, paths })

export const { getNotificationChannels, setNotificationChannel, getPushConfig, subscribePush, unsubscribePush, deliverNotification } = notifications.api
```

`deliverNotification` is internal: the deployment schedules it by that name for each email and push, with ids only, after the event commits. `getPushConfig` answers the public key, `null` until both keys are set.

## Ask a person to allow it

`usePushSubscription()` drives the device side:

```vue [app/components/PushToggle.vue]
<script setup lang="ts">
const { isSupported, isSubscribed, permission, needsInstall, subscribe, unsubscribe } = usePushSubscription()
</script>

<template>
  <p v-if="needsInstall">Add the app to your Home Screen to get notifications.</p>
  <p v-else-if="permission === 'denied'">Allow notifications for this site in your browser's settings.</p>
  <button v-else-if="isSupported && !isSubscribed" @click="subscribe">Notify me on this device</button>
  <button v-else-if="isSubscribed" @click="unsubscribe">Stop notifications on this device</button>
</template>
```

Call `subscribe` from a click: browsers ask for permission only in answer to a gesture. A person who denied permission has to allow it again in the browser's site settings, and `permission` reads `denied` until they do.

What it does on each browser:

- **Safari 18.4 and later**, on macOS and in Home Screen apps on iOS and iPadOS, takes the notification as the message itself (Declarative Web Push), through `window.pushManager`, with no service worker.
- **Chromium, Firefox and earlier Safari** need a service worker to show it. The module serves a small one at `/_backend/push/worker.js` that shows the same message, and opens or focuses the app on a click. Its scope is `/_backend/push/`, so it lives beside a service worker your app already has at `/` and never controls a page.
- **On iPhone and iPad**, push works only once the app is added to the Home Screen, from iOS 16.4. `needsInstall` says when a device must install first ([Make the app installable](https://nuxt-backend.dev/docs/build/email/notifications/installable)).

Every message is the same declarative payload, built in one place: a title, a body and the link it opens, in the person's language. The words are the kind's labels, read from app config's labels (`labels.generated.ts`) and the messages `setupEmail` takes (your i18n files), as the email channel reads them.

## Choose what reaches a device

Push follows the [channel matrix](https://nuxt-backend.dev/docs/build/email/notifications#the-channel-matrix): each kind that allows it reaches every device the person allowed, until they turn that kind's push off in their [notification channels](https://nuxt-backend.dev/docs/build/email/preferences#notification-channels). Billing kinds stay in the app, and a push that would land in the person's quiet hours waits for their end ([Cap how often people hear from you](https://nuxt-backend.dev/docs/build/email/preferences#cap-how-often-people-hear-from-you)). A shared computer belongs to the account that subscribed it last. Signing out drops the device's subscription, so the next person to sign in there doesn't get the last one's notifications.

## Content security policy

The service worker is a script of the app's own origin. The module adds `worker-src 'self'` and `manifest-src 'self'` to the content security policy it sets. Registering the worker then never depends on how a browser reads the policy's fallback ([Security model](https://nuxt-backend.dev/docs/ship/security)).

## Privacy

A device's push address is personal data. It is erased with the account, dropped when the push service says the device is gone, and listed on the [Privacy and data inventory](https://nuxt-backend.dev/docs/ship/compliance/privacy) page.

## Test it

Send yourself one: subscribe a device, then record an event of a kind with `push` among its channels ([Notify people about your own events](https://nuxt-backend.dev/docs/build/email/notifications/custom-events)). The package's own tests send to mocked push services, so make a real round trip on your own devices before launch: a Chromium browser, and Safari or a Home Screen app, each receiving one.

## Related

- [Show in-app notifications](https://nuxt-backend.dev/docs/build/email/notifications): the kinds and the channel matrix.
- [Make the app installable](https://nuxt-backend.dev/docs/build/email/notifications/installable): the Home Screen install iPhone and iPad need.
- [Let people choose their emails](https://nuxt-backend.dev/docs/build/email/preferences#notification-channels): the channels, kind by kind.
- [Notify people about your own events](https://nuxt-backend.dev/docs/build/email/notifications/custom-events): your own kinds with `push` among their channels.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
