Send notifications
You turn notifications on, send one when something happens in your app, and see it in the bell and as push on your browser. This guide uses the ERP example (tests/apps/erp, set up as its README says, with a user from protobase users create): when an order’s status changes, its owner hears about it. The Notifications reference has every option.
1. Turn notifications on
Section titled “1. Turn notifications on”Add notifications to protobase.config.ts:
import { defineConfig } from '@protobase/config'
export default defineConfig({ notifications: {}, // auth, jobs, ...})Then write the tables it needs as a migration of your own, and apply it:
bun run protobase migration # writes db/migrations/NNN_notifications.sqlbun run db:migrateprotobase dev refuses to start until the devices and history tables are there.
2. Generate the Web Push keys
Section titled “2. Generate the Web Push keys”Browsers receive push through Web Push, which needs a key pair of your own:
bun run protobase notifications vapid-keysPaste the two lines it prints into .env, and add how push services can reach you:
PROTOBASE_VAPID_PUBLIC_KEY=...PROTOBASE_VAPID_PRIVATE_KEY=...Keep the private key out of the repository. Without the keys, notifications still reach the bell, and browsers get no push.
3. Send one when a record changes
Section titled “3. Send one when a record changes”Declare the event on the orders, in config/orders/data.ts:
.events((e, r) => [e.updated('statusChanged', { changed: [r.status] })])Then add a subscription that sends a notification to the order’s owner, with a link to the order, and export it from config/index.ts beside the resources:
import { appPath } from '@protobase/schema'import { sendNotification, subscription } from '@protobase/server'
// The app's public origin: the address its sign-in runs on, from its own configuration.const appOrigin = process.env.PROTOBASE_URL ?? 'http://localhost:5173'
/** Tells an order's owner when its status changes, in their inbox and as push, with a link to the order. */export const orderStatusToOwner = subscription('orderStatusToOwner', { events: ['orders.statusChanged'], run: async ({ data }, { db }) => { const owner = data.after?.owner_id if (owner == null) return const url = new URL(appPath({ kind: 'record', resource: 'orders', key: data.key }), appOrigin).href await sendNotification(db, { userIds: [String(owner)], inbox: true }, { title: `Order ${data.after?.number} changed status`, url }) },})export { orderStatusToOwner } from './orders/notifications'inbox: true keeps it in the owner’s history, which the bell shows, besides pushing it; inbox: false only pushes it. The same sendNotification works from a job, a write hook or an API function.
The event needs triggers on the orders table, so write and apply the migration again:
bun run protobase migration # writes db/migrations/NNN_changes.sqlbun run db:migrate4. Turn push on from the bell
Section titled “4. Turn push on from the bell”Run bun run protobase dev, open the address it prints in a desktop browser and sign in. The bell in the top bar opens your notifications; at its foot, Turn on asks the browser for permission and registers this browser as one of your devices. The panel then says that notifications are on for this device.
5. See it arrive
Section titled “5. See it arrive”Make yourself the owner of an order, then change its status, in the app or with SQL: both count, since the change is captured in the database. Within a few seconds:
- the browser shows a notification such as “Order SO-2024-000245 changed status”, and a click on it opens the order;
- the bell shows one unread notification, and opening it marks it read.
Every send is a background job: a failed one is on the failed jobs page, and the server log has a line per delivery of the subscription.
On an iPhone or iPad, push reaches the app once it is added to the Home Screen, not a browser tab. The Android and iOS builds of your app receive push too, set up as the mobile guide shows.
6. Turn the bell off or replace a part
Section titled “6. Turn the bell off or replace a part”notifications: { ui: false }keeps sending, the history and devices, and draws no bell: your own UI reads the history and turns push on with the browser helper (without the bell).shell.bellinprotobase.ui.tsxdraws your own bell in its place (the bell’s slot).- Your own device store, history store or senders replace the built-in ones, such as a store outside the database (your own parts).
- The tables’ schema, names and columns can be renamed (table names).