Tutorials
Follow a notification from a registered device to delivery. The device registry identifies the recipient, the notification defines the message and a provider handles transport.
Tip · Where to go next
This page is the happy path. Once it works, the reference pages carry the detail: Notification Delivery (which channels a type may use, and why one was dropped), Devices (the registration API a client app calls), Push Notifications (transport, retries, error codes, environment variables), and the admin guide for the screens.
You want a message to pop up on a user's phone. Four small modules cooperate to make that happen:
| Module | One-line job | Think of it as… |
|---|---|---|
devices | Remembers each user's phones/tablets and their push tokens | The address book of devices |
notifications | Defines notification types, stores in-app messages, and tracks per-user opt-out preferences | The content + rules |
push_notifications | Takes a notification and actually delivers it to devices (queue, retries, delivery log) | The mail carrier |
communication_channels (+ channel-fcm / channel-apns / channel-expo) | Talks to Apple / Google / Expo using your credentials | The post office for each platform |
Flow in one sentence:
Your app registers a device → later your backend creates a notification → the push module looks up the user's devices → and hands each one to FCM/APNs/Expo.
[Mobile app] [Your backend]
| |
register device create notification
| |
v v
devices module <---------- push_notifications ----> communication_channels
(token stored) (find devices, queue) (FCM / APNs / Expo)
|
v
📱 push lands
devices module – the address bookOne row per user device (a UserDevice). Important fields:
userId, platform (ios | android | web)deviceId – a stable id the app generates (e.g. iOS identifierForVendor, Android ID, or a UUID for web)pushToken – the token from Apple/Google/Expo. Encrypted at rest, never returned by any API.pushProvider – fcm | apns | expolastSeenAtPOST /api/devices
Authorization: Bearer <user-token>
{
"deviceId": "A1B2-C3D4",
"platform": "ios",
"pushToken": "<token from APNs/FCM/Expo>",
"pushProvider": "apns",
"clientAppVersion": "1.2.3",
"osVersion": "17.5"
}
(user, deviceId) just updates the row.PUT /api/devices/:id) whenever the push token rotates.GET /api/devices – list your own devicesDELETE /api/devices/:id – deactivate (soft-delete) your own device/api/devices/admin/devices... – admin endpoints to manage devices across usersdevices.view – see your own devicesdevices.manage – register/update/delete your owndevices.admin – manage everyone's devices in the tenantnotifications module – content + rulesEvery notification has a type (a frozen id like sales.order.created). A module declares its
types in a notifications.ts file. A type carries:
titleKey / bodyKey – i18n keys for the textcategory – grouping for UI (e.g. orders, security)silent – if true, it's a background wake-up (delivery style only: no visible message). It still respects the user's preferences – a silent type that must always fire must also be nonOptOut.nonOptOut – if true, the user cannot turn it off (e.g. security alerts); this is the only flag that bypasses the preference gateUsers can disable a type per channel (in-app, push, …):
GET /api/notifications/preferences / PUT /api/notifications/preferencesnonOptOut types ignore preferences – they always go through. silent types do respect opt-out (silent controls delivery style, not enforcement), so a silent type that must always fire must also be nonOptOut.Notification types can declare which channels they may deliver on. Every user-configurable
built-in type does so explicitly: business and actionable types ship
channels: ['in_app', 'email'], while the demo.silent_ping and demo.push_playground fixtures
ship channels: ['push']. Push is therefore off for ordinary business notifications out of the
box, so connecting a push provider does not blast the entire catalogue to phones. A channel
outside the type's set never delivers and users cannot enable it in their preferences (the cell
shows as locked).
Two ways to change the set:
channels: ['in_app', 'email', 'push'] (or omit the
field ⇒ every channel) on the NotificationTypeDefinition.PATCH /api/notifications/types with
{ "id": "messages.new", "channels": ["in_app", "email", "push"] } (pass channels: null to go
back to the code-declared set). The override is scoped to the operator's tenant – other
tenants keep their own settings – and replaces the code default in both directions: enable push
for a type that ships without it, or switch any channel off entirely.Registering a new channel does not automatically make existing built-in types eligible for it. Channel authors should declare it on notification types their module owns; operators can add it to other types through the tenant-scoped override.
Channels inside the set behave as usual: users opt in/out per type, and nonOptOut types always
deliver. The eligibility check runs before both – a channel outside the set is off even for
nonOptOut types.
POST /api/notifications
{
"recipientUserId": "<uuid>",
"type": "sales.order.created",
"titleKey": "notifications.sales.order_created.title",
"bodyKey": "notifications.sales.order_created.body",
"bodyVariables": { "orderNumber": "12345" },
"linkHref": "/backend/sales/orders/<uuid>",
"data": { "orderId": "<uuid>" }, // custom fields delivered inside the push
"pushOptions": { "sound": "default", "badge": 1 }
}
Bulk variants exist too: /api/notifications/batch, /role, /feature.
From code (the common, event-driven way), resolve the service via DI:
const notificationService = ctx.resolve('notificationService')
await notificationService.create(input, { tenantId, organizationId })
push_notifications module – the mail carrierYou usually don't call this directly. When a notification is created, a delivery strategy runs automatically and:
nonOptOut; silent types are still preference-gated).PushNotificationDelivery row (status pending) per device.Observability for admins:
GET /api/push_notifications/deliveries – the delivery log (status, attempts, errors).There is no dedicated silent-push primitive. A silent push is just a normal notification whose
type is declared silent: true – create it through the ordinary notificationService.create()
path and the push delivery strategy sends a data-only, content-available wake-up (no banner):
// type declaration (module notifications.ts): { type: 'system.security.new_login', silent: true, nonOptOut: true, ... }
const notificationService = ctx.resolve('notificationService')
await notificationService.create({
recipientUserId: userId,
type: 'system.security.new_login', // registered with silent: true
title: 'New login', // required; not shown for a silent push
data: { ipAddress: '203.0.113.42' }, // delivered inside the push payload
}, { tenantId, organizationId })
Because silent types respect preferences, mark the type nonOptOut: true if it must always fire.
For an admin one-off visible push, use pushNotificationService.sendCustomPush(...) (or the
Send Push admin page).
Push tokens are useless without credentials. Credentials live per tenant in the admin UI (no env vars): Settings → Module Configs → Communication Channels → Connect.
| Provider | Platforms | What you paste | Where to get it |
|---|---|---|---|
| Firebase (FCM) | Android + iOS | Service-account JSON | Firebase Console → Project settings → Service accounts → Generate key |
| Apple (APNs) | iOS | .p8 key + Key ID + Team ID + Bundle ID + production flag | Apple Developer → Keys → APNs key |
| Expo | Android + iOS | (optional) access token | expo.dev → Account → Tokens |
A device is routed to a provider by the pushProvider it registers with, never by its platform.
So FCM serves iOS as well as Android – the adapter emits the apns payload block (priority
headers, aps.sound/aps.badge, and content-available: 1 for silent pushes) alongside the
android block on every send.
Choosing between FCM and APNs for iOS:
Connecting both is fine: each device is delivered through whichever provider it registered with.
These modules ship new ACL features (devices.*, push_notifications.view_deliveries, and the
channel_fcm / channel_apns / channel_expo grants). New tenants pick them up automatically
during setup, but existing tenants only receive newly declared grants after you run the
idempotent sync command once:
yarn mercato auth sync-role-acls
Without it, an admin on an existing tenant may not see the push delivery log or the provider
connect UI because their role is missing the freshly added grants. Use --tenant <tenantId> to
target a single tenant.
POST /api/devices with pushToken + pushProvider.PATCH /api/notifications/types). The demo.silent_ping and demo.push_playground fixtures
already ship with push eligibility. The recipient must also not have opted out of push for that
type.POST /api/notifications with recipientUserId + type.GET /api/push_notifications/deliveries.silent: true.For the rules that decide whether a channel delivers at all – per-type eligibility, operator overrides, non-opt-out, and the default that makes a type without a declared channel list push-eligible – see Notification Delivery.
Every OM_PUSH_* variable is optional; push works with none of them set. The full table, including
the reclaim and receipt-polling knobs and the two test-only provider flags, lives in
Push Notifications → Environment variables.
Both test flags must reach the worker process as well as the app, because the push worker runs in its own process.
Previous: Material Availability Check · Next: Tutorial - build your first Open Mercato app