Push Notifications
| Question | Flag |
|---|---|
| Add push notifications? | --push / --no-push |
--push delivers pushes to the PWA and, with --mobile, to the native apps, through Firebase Cloud Messaging. Supabase keeps each device's token and sends: inserting a row in notifications is what sends a push.
insert into public.notifications (user_id, title, body, url)
values ('<user id>', 'Hello', 'You have a new message', '/');It generates a Supabase migration, the push-notify Edge Function, a service worker for web pushes and a permission prompt. Push turns on PWA support, since web pushes need its service worker, and with --mobile it also brings live and mandatory native updates.
The permission prompt appears once someone is signed in, since a token belongs to a user: use it with --auth. Declining is remembered on the pushPrompt entity. To sign out, call signOut from src/api/push, which deletes the device's token first.
Setup
With a Firebase project as in Analytics and the Supabase CLI:
VITE_FIREBASE_VAPID_KEYis the public half of a Web Push key pair, from Project settings → Cloud Messaging → Web configuration.- In the Firebase console, go to Project settings → Service accounts → Generate new private key. The file is a real secret: never commit it.
- In the Google Cloud console, give that service account the Firebase Cloud Messaging API Admin role. The similarly named Firebase Cloud Messaging Admin cannot send.
supabase init, thensupabase startto try it locally, with the file's JSON on one line asFCM_SERVICE_ACCOUNTinsupabase/functions/.env.
To deploy:
supabase link --project-ref <your project ref>
supabase db push
pnpm secrets:push
supabase functions deploy push-notify
pnpm vault:pushpnpm secrets:push sets the secrets in supabase/functions/.env, such as FCM_SERVICE_ACCOUNT, on the hosted project and checks that each arrived as it is locally; pnpm secrets:push FCM_SERVICE_ACCOUNT sets just that one. pnpm vault:push tells the database how to reach the function, storing the project's service_role key in Vault. Both come from @inglorious/supabase.
A browser's service worker only exists in a build, so try web pushes with pnpm build && pnpm preview. The API behind it is createPushPrompt.
