Send a notification
Tell a person about something your module did, in the app and optionally by email, while respecting their notification preferences. Never send mail by hand.
- Type the notification's data in
src/contracts/notifications.ts:declare module "@monark/notifications/contracts" { interface NotificationDataRegistry { "posts.post-published": { postId: string; postTitle: string }; } } export {}; - Register the kind with English and French templates in
src/server/notification-kinds.ts:
Call the helper inregisterNotificationKind( "posts.post-published", { category: "ACTIVITY", channels: ["IN_APP", "EMAIL"], defaultEnabled: { IN_APP: true, EMAIL: false }, requiredEmail: false, template: "posts/post-published", }, { en: { subject: "Published: {{ postTitle }}", html: "<p>…</p>", text: "…", inapp: { subject: "Published: {{ postTitle }}", body: "…", link: "/posts/{{ postId }}" } }, fr: { /* same shape, in French */ }, }, );services/api/src/server.ts, in the notification-kinds block. - Send it from a subscriber to your domain event, not from the procedure:
Register the subscriber in the subscribers block, after the notification kinds and before the webhook subscriber.import "../contracts/notifications"; on<PostPublishedEvent>("posts.post-published", async (event) => { const post = await findPostById(event.postId); if (!post || post.authorId === event.actorId) return; await notify("posts.post-published", { userId: post.authorId }, { postId: post.id, postTitle: post.title }); }); - Trigger the event as another user and open the notifications in the user menu. With email on, the message lands in the local mail catcher (
pnpm dev:tools:mail).
Good to know:
- Don't notify people about their own actions.
notifynever throws; failures are recorded and logged, so the action that caused it is never rolled back.- Set
requiredEmail: trueonly for account-safety messages, which people can't turn off.