Skip to content

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.

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:

Terminal window
bun run protobase migration # writes db/migrations/NNN_notifications.sql
bun run db:migrate

protobase dev refuses to start until the devices and history tables are there.

Browsers receive push through Web Push, which needs a key pair of your own:

Terminal window
bun run protobase notifications vapid-keys

Paste the two lines it prints into .env, and add how push services can reach you:

Terminal window
PROTOBASE_VAPID_PUBLIC_KEY=...
PROTOBASE_VAPID_PRIVATE_KEY=...
PROTOBASE_VAPID_SUBJECT=mailto:[email protected]

Keep the private key out of the repository. Without the keys, notifications still reach the bell, and browsers get no push.

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:

config/orders/notifications.ts
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 })
},
})
config/index.ts
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:

Terminal window
bun run protobase migration # writes db/migrations/NNN_changes.sql
bun run db:migrate

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.

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.

  • 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.bell in protobase.ui.tsx draws 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).