# How push works

> How new-mail notifications reach phones, why Postern needs a push relay for that, and how to point your Hub at one.

Source: https://docs.posternmail.app/relay/

This page explains how a new message on your Hub becomes a notification on your phone, what
the push relay is for, and what you set on the Hub to use one. To run a relay yourself, see
[Run your own relay](https://docs.posternmail.app/relay/run-your-own/). For exactly what passes through it, see
[What the relay sees](https://docs.posternmail.app/relay/what-it-sees/).

> [!IMPORTANT]
> The official relay is **`https://relay.posternmail.app`**. It reaches the Postern
> app from the App Store and TestFlight on iPhone, iPad and Apple Watch: set
> `POSTERN_RELAY_URL=https://relay.posternmail.app` on your Hub
> ([how](https://docs.posternmail.app/relay/#set-the-relay-url-on-your-hub)). It does not reach an app you built yourself, and
> it does not reach Android yet (no Firebase project exists for it). For those, you run
> [your own relay](https://docs.posternmail.app/relay/run-your-own/) with credentials that match your build.

## Why a relay exists

Phones only show push notifications that come through their platform's push service:

- **iOS** accepts pushes only from the Apple Push Notification service (APNs).
- **Android** accepts pushes only from Firebase Cloud Messaging (FCM).

Apple and Google only accept a push for an app if it is signed with that **app developer's**
credentials: an APNs key from the Apple developer team that owns the app's bundle ID, and a
Firebase service account of the Firebase project the app was built with. Those credentials
cannot be handed out to every Hub. The relay is the one place that holds them. Your Hub sends
it a short message ("this device has new mail"), and the relay signs it and passes it on to
Apple or Google.

The relay is a separate small program (`postern-relay`, in the repository's `relay/`
folder; the official one runs the same API as a Cloudflare Worker, `deploy/cloudflare-relay/`).
It has no access to your mailboxes, keeps no list of devices and stores no messages.

Browsers and the desktop app do not need the relay (see
[Other ways notifications arrive](https://docs.posternmail.app/relay/#other-ways-notifications-arrive)).

## From new mail to the phone

```text
Mail provider ──new mail──▶ Hub ──"device X: new mail"──▶ Push relay ──▶ APNs / FCM ──▶ phone
                             ▲                                                          │
                             └──────────────── the app opens and fetches mail ◀─────────┘
```

1. **The Hub notices new mail.** It watches your mailboxes even when your apps connect to
   the providers directly.
2. **The Hub decides whether to notify.** Your notification settings apply first: **Notify me
   about** (all mail, VIPs only, nothing), quiet hours, and per-mailbox choices. See
   [Notifications](https://docs.posternmail.app/using/notifications/).
3. **The Hub picks the devices.** Every signed-in device that has notifications turned on
   gets one. A phone that registered for push gets a push message even while its app is
   connected, because the system may have suspended the app without closing its connection.
   An open browser tab or desktop app gets the notification over its live connection instead.
4. **The Hub builds the payload.** With previews on, it contains the sender, the subject and
   the first words of the message. With previews off, it contains only a generic title such
   as "New mail". See [Previews on or off](https://docs.posternmail.app/relay/#previews-on-or-off).
5. **The Hub sends it to the relay** (`POST /v1/push`), authenticated with the secret it got
   when it registered. Up to 100 messages go in one request.
6. **The relay forwards each message** to APNs (iOS) or FCM (Android) and reports back per
   device whether it was delivered. If Apple or Google say a device token is no longer valid,
   the Hub deletes that device's push registration.
7. **The phone shows the notification.** Tapping it opens the conversation; the app fetches
   the message itself, from your Hub or straight from your provider.

A device registers for push when you turn on notifications in the app (**Settings →
Notifications**). The app asks the system for permission, gets a device token from Apple or
Google, and sends that token to your Hub. The token is stored on the Hub, not on the relay.

## Set the relay URL on your Hub

`POSTERN_RELAY_URL` is the base address of the relay, for example
`https://relay.example.com`. It does three things:

- It turns on mobile push. Without it, the Hub refuses push registrations from the apps, and
  turning on notifications in the app shows "Your Hub isn't set up to send notifications
  yet. Whoever runs it can turn that on in the Hub's settings."
- It tells the Hub where to register. On the first push, the Hub calls `POST /v1/register`
  on the relay, receives a Hub ID and a secret, and stores the secret encrypted with the
  master key. It registers again by itself if you change the URL, or if the relay no longer
  accepts its secret.
- It is checked for health. The Hub calls `GET /healthz` on the relay for the status page and
  for `postern-hub doctor`.

Leave it empty (the default) to turn mobile push off.

**Docker Compose:**
Add the line to `~/postern/.env` (compose.yaml passes it through), then apply it:

```ini
POSTERN_RELAY_URL=https://relay.example.com
```

```sh
cd ~/postern && docker compose up -d
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Add the line to `/etc/postern/hub.env`, then restart the Hub:

```ini
POSTERN_RELAY_URL=https://relay.example.com
```

```sh
sudo systemctl restart postern-hub
```
**Desktop app (built-in Hub):**
Not available. The desktop app's built-in Hub is only reachable from that computer, so
phones cannot sign in to it and there is nothing to push to. Run the Hub with Docker or as a
systemd service if you want phone notifications.

To check it, run the doctor:

**Docker Compose:**
```sh
cd ~/postern && docker compose exec hub postern-hub doctor
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
```sh
sudo -u postern sh -c 'set -a; . /etc/postern/hub.env; exec postern-hub doctor'
```
**Desktop app (built-in Hub):**
Not available for the built-in Hub.

> **Check:** The doctor prints a line like `push relay https://relay.example.com is
> healthy`. In the web app, **Settings → Admin → Status → Phone notifications** says
> **Working**.

> **If it did not work:** **Status** shows **Can't be reached** and the doctor prints
> `push relay ... is unreachable` when `GET /healthz` on the relay does not answer with
> HTTP 200 within 5 seconds. Open `https://relay.example.com/healthz` from the Hub's machine
> (for example with `curl`) and check DNS, the reverse proxy in front of the relay and its
> certificate. **Not set up** means the Hub does not see `POSTERN_RELAY_URL`: check the
> spelling and that you restarted the Hub.

### What the status values mean

| Where | Value | Meaning |
|---|---|---|
| **Settings → Admin → Status → Phone notifications** | **Not set up** | `POSTERN_RELAY_URL` is empty. |
| | **Working** | The relay's `/healthz` answered with HTTP 200 (checked at most every 5 minutes). |
| | **Can't be reached** | The relay did not answer. The status page also warns: "The push relay is unreachable: mobile notifications are delayed." |
| `postern-hub doctor` | `Push relay disabled (POSTERN_RELAY_URL not set)` | Push is off. |
| | `push relay <url> is healthy` | `/healthz` answered with HTTP 200. |
| | `push relay <url> is unreachable` | Anything else. |

A healthy relay only proves the relay is up. Whether a push actually reaches a phone also
depends on the relay's Apple or Google credentials matching your app build. See
[Run your own relay](https://docs.posternmail.app/relay/run-your-own/#apple-and-google-credentials).

## Previews on or off

Each user decides how much a phone notification shows. The setting applies to all their
phones:

- Web app: **Settings → Notifications → Show sender and subject on phones**
- Mobile app: **Settings → Notifications → Show previews on phones**

It is **on** by default.

| | Previews on (default) | Previews off |
|---|---|---|
| Notification title | Sender name (or address) | "New mail" |
| Second line | Subject, or "(no subject)" | none |
| Text | The first words of the message (up to 240 characters) | none |
| Sender's address | Sent, so iOS can show the sender's picture | Not sent |

With previews off, other kinds of notification also get a generic title: "Follow-up
reminder", "A snoozed conversation is back", "A message could not be sent" or "Your mail
needs attention".

With previews on, the sender, subject and first words pass through the relay and through
Apple's or Google's push service. With previews off, none of that text leaves your Hub in a
push. [What the relay sees](https://docs.posternmail.app/relay/what-it-sees/) lists every field.

## Other ways notifications arrive

### Web Push (browsers)

Browsers get notifications through Web Push, without the relay. The Hub creates its own Web
Push key pair (VAPID) on first start and keeps the private key encrypted with the master key.
In the web app, turn it on under **Settings → Notifications** in the **This device**
section. Web Push needs the Hub to be opened over HTTPS.

The message is encrypted end to end to your browser, so the browser vendor's push service
carries it but cannot read it. It contains the sender, the subject and the first words of
the message; the "previews on phones" setting does not apply to it. The push service does see
your Hub's public address as the sender contact (or, when the Hub has no HTTPS public URL, the
email address of the first admin).

### Desktop app

The desktop app shows notifications itself while it is running, including when its window
is closed to the tray. It needs no relay and no Web Push.

### UnifiedPush

The Hub can deliver to a [UnifiedPush](https://unifiedpush.org/) endpoint (for example a
self-hosted ntfy server) without the relay. The payload is the same as for the relay, and the
previews setting applies. The endpoint must be an `https://` address; it may be on your LAN or
tailnet, but not on loopback or link-local addresses.

The Postern Android app does not register a UnifiedPush endpoint yet, so today this only
works for custom clients that call the Hub's API (`POST /api/v1/devices/current/push` with
`"kind": "unifiedpush"`).

### Without any push

Without a relay, phones get no push notifications. The app shows new mail when you open it.

## Common problems

| What you see | Cause | What to do |
|---|---|---|
| "Your Hub isn't set up to send notifications yet. Whoever runs it can turn that on in the Hub's settings." | `POSTERN_RELAY_URL` is not set on the Hub. | [Set the relay URL](https://docs.posternmail.app/relay/#set-the-relay-url-on-your-hub). |
| "Notifications are turned off for Postern in the Settings app." | The system permission was denied. | Allow notifications for Postern in the phone's Settings app, then turn them on in Postern again. |
| "Notifications need the installed Postern app; they don't work in Expo Go on Android." | You are running a development build in Expo Go. | Install a real build of the app. |
| **Phone notifications: Working**, but nothing arrives | The relay's credentials do not match your app build, or previews and mode settings filter it out. | See [Run your own relay](https://docs.posternmail.app/relay/run-your-own/#troubleshooting). |
