MIT · TypeScript · version 1.0.3
direct-push
Push notifications from your own backend. Straight to Apple, Google and the browsers.
$ npm install direct-push- 0
- dependencies
- 3
- services: APNs, FCM, Web Push
- 4
- runtimes: Deno, Node, Bun, Workers
- 3
- databases: Postgres, SQLite, D1
Quick start
- 01
npx direct-push initWhere the server runs. It asks for the .p8 and the service account, tries them against Apple and Google, makes the Web Push keys and writes the .env. On Supabase or Cloudflare it also uploads them as secrets.
- 02
createPushServer({ sql, push })The routes your apps call. It keeps each user’s devices and queues the sends, with retries.
- 03
npx direct-push appIn each app’s folder. It finds the iOS, Android and web projects and adds the client.
$
The server
A Supabase Edge Function, a Node process or a Worker, on the database you already have.
supabase/functions/push/index.ts
import postgres from "npm:postgres@3";
import { pushFromEnv } from "npm:direct-push@1";
import { createPushServer } from "npm:direct-push@1/server";
import { postgresJsSql } from "npm:direct-push@1/postgres";
const server = createPushServer({
sql: postgresJsSql(postgres(Deno.env.get("SUPABASE_DB_URL")!, { prepare: false })),
push: pushFromEnv((name) => Deno.env.get(name)),
authorize: signedIn, // your session: the user's id, or null
cronSecret: Deno.env.get("PUSH_CRON_SECRET"),
});
Deno.serve((req) => server.fetch(req));The apps
One client per platform: it registers the device with your server and removes it on sign-out.
AppDelegate.swift
import DirectPush
// didFinishLaunching
DirectPush.shared.start()
// didRegisterForRemoteNotificationsWithDeviceToken
DirectPush.shared.didRegister(deviceToken: token)
// Signed in
let devices = URL(string: "https://api.example.com/push/devices")!
DirectPush.shared.register(to: devices) {
["Authorization": "Bearer \(try await session.accessToken())"]
}
await DirectPush.shared.requestAuthorization()Where it runs
| Runtime | APNs | FCM, Web Push | Import |
|---|---|---|---|
| Deno · Supabase | ✓ | ✓ | npm:direct-push |
| Node 22.18+ · Bun | ✓ HTTP/2 | ✓ | direct-push/node |
| Cloudflare Workers | — no HTTP/2 out | ✓ | direct-push/worker |
| Browsers | subscribe, show | direct-push/web · /sw |
Devices are kept in Postgres (Supabase, Neon), SQLite or Cloudflare D1: the database you already have.
What it takes care of
APNs environments, learnt
Xcode builds are sandbox, TestFlight is production. A token of unknown environment is tried in both, and the result says which took it.
Dead means certainly dead
410, Unregistered, a push service’s 404: deleted. 429, 5xx, a timeout: retried. A key or topic of ours: reported, never deletes a device.
A wrong setting can’t wipe your devices
A wrong bundle id makes every token look dead. Until that setting has delivered once, those answers say config, not dead.
At least once
The outbox leases what it sends and writes the outcome after: a crash or a 503 sends it again later. A dedupe key keeps a job that runs twice from queueing twice.
One token, one connection
Each key signs once however many pushes ask at once, and on Node keeps one HTTP/2 connection to APNs, as Apple asks.
The user’s switch
Each device has an on/off in the app. Off stays registered and is sent nothing; the OS permission withdraws and restores it.
Why
APNs, FCM and Web Push are HTTP APIs: a JWT signed with your key and a POST. None of them needs an SDK or a service in between, and sending straight means their answers come back to you: which token is gone, which environment it belongs to, which key is wrong.
Sending is the easy part. The work is reading those answers without deleting good devices, and that is most of the code: the list is above.
Without
- A third-party push service
- The Firebase Admin SDK
- Expo’s push service
- Certificates that expire: a .p8 key doesn’t, and covers every app of the team
GarageApp and Skedler send their pushes with it.