Self-hosting the push relay
Only Apple can deliver a notification to an iOS device, and only a holder of an Apple developer signing key can ask it to. Your controller has neither, so it hands the sealed notification to a relay that does.
By default that is the hosted one. Running your own takes it out of the path, so notifications go through your Apple developer account and no infrastructure but yours. Nothing about the privacy of a notification changes — the payload was sealed before it left your controller either way — but who sees that one happened does.
→ Notifications for the shape of the path. → Push privacy for what a relay can and cannot see.
What you need from Apple
Section titled “What you need from Apple”A .p8 APNs signing key from the Apple developer portal, its 10-character key
id, your team id, and the app’s bundle id as the topic.
The relay picks the APNs worker from each push’s environment field, so one process serves both sandbox and production device tokens, and the signing key follows the same field.
A key scoped to both environments is all you need, and --apns-key-p8 alone
covers it. A key scoped to production alone cannot sign sandbox sends — APNs
answers those with 403 BadEnvironmentKeyInToken — and the fix is a second key:
pm pushgw \ --apns-key-p8 /etc/puppet-master/apns-key.p8 \ --apns-key-id XXXXXXXXXX \ --apns-sandbox-key-p8 /etc/puppet-master/apns-sandbox-key.p8 \ --apns-sandbox-key-id ZZZZZZZZZZ \ --apns-team-id YYYYYYYYYY \ --apns-topic com.example.appEach key mints and caches its own provider token, so neither environment is ever handed the other’s. Both sandbox flags are set together or not at all: a path with no id is rejected at startup rather than quietly falling back to the primary key, which would leave sandbox pushes failing in exactly the way the second key exists to fix.
There is no separate team id — APNs keys belong to a team, so both use
--apns-team-id.
Pointing a controller at it
Section titled “Pointing a controller at it”pm config set push.gateway.url https://pushgw.example.compm config unset push.gateway.url goes back to the hosted relay. Setting it to
empty delivers nothing at all, which is the way to turn notifications off
entirely without touching the event settings.
Running it
Section titled “Running it”Every flag has an environment variable, which is how the unit in the repository is configured.
| Flag | Environment | Default |
|---|---|---|
--listen |
PUSHGW_LISTEN |
127.0.0.1:8400 |
--apns-key-p8 |
PUSHGW_APNS_KEY_P8 |
required |
--apns-key-id |
PUSHGW_APNS_KEY_ID |
required |
--apns-team-id |
PUSHGW_APNS_TEAM_ID |
required |
--apns-topic |
PUSHGW_APNS_TOPIC |
required |
--apns-sandbox-key-p8 |
PUSHGW_APNS_SANDBOX_KEY_P8 |
the primary key |
--apns-sandbox-key-id |
PUSHGW_APNS_SANDBOX_KEY_ID |
the primary key |
--apns-endpoint |
PUSHGW_APNS_ENDPOINT |
the standard workers |
The relay keeps nothing on disk: no registrations, no device tokens, no database. It reads the APNs key once at startup and writes nothing afterwards, which is why its unit can mount the filesystem read-only and grant no capability. Rate counters live in memory and are lost on a restart, which is harmless — a controller retries a delivery across about three-quarters of an hour.
It serves two endpoints: POST /v1/push and GET /health.
Put TLS in front of it
Section titled “Put TLS in front of it”The relay listens on loopback by default and should stay there, with a reverse
proxy terminating TLS in front. The repository carries a systemd unit, an
example environment file and a Caddy configuration under deploy/pushgw/, plus
a Dockerfile and a make pushgw-image target for running it as a container.
Two things before you deploy it:
- Terminating TLS puts the proxy inside the trust boundary. A push request carries no signature, so anything that can rewrite it in flight can point it at a different device token or change the environment.
- The per-source-address rate limit collapses behind a proxy. It keys on the TCP peer, which is the proxy for every request, so what was one bucket per sender becomes one bucket shared by all of them. The per-device-token limit is unaffected. Reaching the relay directly keeps both limits per sender.
The supplied Caddy configuration serves the name over HTTPS only. An https://
site address is not enough on its own, because Caddy still opens a port 80
listener to redirect, so the file disables those redirects globally — which
applies to every site that Caddy instance serves and is worth checking before
dropping it into an existing configuration. With nothing on port 80, certificate
issuance goes through the TLS-ALPN-01 challenge, so port 443 has to be reachable.
The thing to understand before you run one
Section titled “The thing to understand before you run one”The relay authenticates nothing. It accepts a push from any caller, and the device token is the bearer credential: whoever holds a token can send to that device.
That is the same model comparable push gateways use, and abuse is bounded with rate limits rather than a sender gate. But an open relay sends through your Apple developer account, so sustained abuse is absorbed by it. If you need to restrict who may relay, do it in front of the process — a network policy, or an authenticating proxy — because the relay has no such control of its own.
What an attacker holding a token still cannot do is read or forge a notification’s content: the payload is sealed to the device, and every one is padded to the same length, so its size says nothing either. They can produce generic banners.
Checking it
Section titled “Checking it”The startup line names the identity it signs under — topic, key id, team id,
sandbox key id and endpoint — so which keys loaded is readable without
inspecting the process arguments. sandbox_key_id reads same-as-primary when
no sandbox key is configured.
After that it logs one line per push outcome. No key material is ever logged, and an outcome line carries a short hash of the device token rather than the token.
A key file the relay cannot read fails at startup saying so. In a container the
process runs as an unprivileged user, so a key left at mode 0600 owned by
another user is the usual cause.