Concepts & Architecture
Overview
Section titled “Overview”Pragmatic.Notifications is a channel-agnostic notification pipeline. The caller constructs a NotificationRequest describing what to send and to whom; the pipeline decides how to deliver it.
The design separates three concerns:
- Recipient resolution – translating logical targets (user ID, role, tenant) into concrete addresses
- Channel routing – selecting delivery channels based on priority, preferences, and registered providers
- Delivery tracking – persisting every delivery attempt with lifecycle status
Pipeline Stages
Section titled “Pipeline Stages”NotificationRequest | 1. Validate Checks Subject and Body are non-empty | 2. Resolve IRecipientResolver: logical recipient -> RecipientResolution (resolved + unreached) | 3. Route INotificationRouter: priority + preferences -> NotificationChannel flags | 4. Deliver INotificationChannel.DeliverAsync per channel per recipient | 5. Track INotificationStore: create record, update status (Sent/Failed) | NotificationResult1. Validate
Section titled “1. Validate”Minimal validation: Subject and Body must be non-empty. If validation fails, the pipeline returns NotificationResult.Failed(...) immediately. No records are created.
2. Resolve
Section titled “2. Resolve”IRecipientResolver.ResolveAsync converts a NotificationRecipient into one or more ResolvedRecipient records. Each resolved recipient has:
Address– concrete delivery target (email, URL, phone number)Channel– the channel inferred from the address typeLocale– for future localized contentTenantId– for multi-tenant routingPreferences– user notification preferences (if available)
It returns a RecipientResolution: the resolved recipients, and the recipients it could not reach (Unreached), which NotificationResult.Unreached reports beside what was delivered to the others.
The DefaultRecipientResolver takes direct addresses (email, phone, webhook) as they are. It resolves a user ID, several, or a role through the IRecipientDirectorys registered (in Abstractions, several, each asked in turn):
- local accounts answer a user’s e-mail;
Pragmatic.Authorization.Managementanswers a role’s members, in force, in the request’s tenant or global.
Each member is reached once. A user no directory knows, a role no directory has members for, and a tenant recipient, which no directory resolves, are named as unreached rather than dropped. For users kept elsewhere, implement IRecipientDirectory; to decide resolution entirely, register your own IRecipientResolver.
3. Route
Section titled “3. Route”INotificationRouter.Route determines which channels to use for each resolved recipient. The DefaultNotificationRouter
chooses only among the channels the recipient’s address belongs to: its ResolvedRecipient.Channel, narrowed to the
registered ones. Priority decides how many of those it uses:
| Priority | Strategy, among the registered channels the address belongs to |
|---|---|
Critical |
All of them |
High |
The preferred channel, Email and Push among them; all of them when none of those is |
Normal |
All of them |
Low |
One: the preferred channel, else Email (digest candidate), else the first |
An address is never handed to a channel it does not belong to: a Critical notification to an e-mail address is one
e-mail, even with SMS and webhooks registered. A notification reaches several channels through several addresses, one
resolved recipient each (EmailAddress and PhoneNumber on the same NotificationRecipient resolve to two). The rule is
the same for every channel, Push and InApp included: a resolver whose push channel looks the user up by e-mail address
says so, by resolving that address for Email | Push (the channel is a flags value). A recipient no registered channel
reaches is skipped and logged; when none is reached, the result fails and says so.
Before routing, preferences are checked:
- If
Preferences.Enabledisfalse, returnsNotificationChannel.None(skip) - If the
Categoryis inPreferences.MutedCategories, returnsNotificationChannel.None
The ChannelOverride property on NotificationRequest bypasses routing entirely. It pins channels, not
addresses: each resolved recipient carries the channel its address belongs to (an e-mail address
Email, a phone number Sms, a URL Webhook), and receives only the overridden channels it shares.
An override of Email | Sms sent to an e-mail address and a phone number delivers one e-mail and one
SMS. A recipient with no address for any overridden channel is skipped and logged; when no recipient
has one, the result fails and says so.
4. Deliver
Section titled “4. Deliver”For each resolved recipient, the pipeline iterates over individual channel flags and calls INotificationChannelFactory.GetChannel(flag, tenantId). If a channel provider is registered, it calls INotificationChannel.DeliverAsync.
Each delivery attempt returns a DeliveryResult(Success, ProviderId, ErrorMessage). The pipeline collects errors and delivery IDs.
Partial success: if some deliveries succeed and some fail, the result is still Success = true with the errors attached. Only when all deliveries fail does the result become Success = false.
Zero deliveries is a failure. If nothing was even attempted (no channel is registered for the
resolved channels, or the recipient’s preferences suppressed every one), the result is Success = false
and the error distinguishes the two causes. Reporting success there would tell the caller the
notification went out while it silently evaporated, which is precisely what an app that forgot to
register a channel needs to be told. The tracking record for the same operation is settled to Failed
in that case, so result and record always agree.
5. Track
Section titled “5. Track”Before each delivery attempt, a NotificationRecord with Status = Pending is created via INotificationStore.CreateAsync. After delivery, the status is updated to Sent or Failed.
The record includes: audience, recipient address, channel, subject, category, tenant ID, metadata, and lifecycle timestamps (CreatedAt, SentAt, DeliveredAt, ReadAt).
The records are kept in the database of the boundary marked [StoresNotifications], in __Notifications. Which package provides the store follows the boundary’s engine:
| Engine | Package | The store |
|---|---|---|
| EF Core | Pragmatic.Notifications.EFCore |
UseEfCoreStore, pointed at that boundary’s database; the generated context maps the table |
| Sql, documents | Pragmatic.Notifications.Engines |
registered by the boundary’s generated Add{Boundary}Persistence, over the boundary’s session; the table (or collection) is in the engine’s schema |
The table is the same on both, column for column and index for index, so one database holds one __Notifications whichever engine writes it. On the documents engine the instants are stored as UTC ticks, since a BSON date keeps milliseconds only. Without the package of the boundary’s engine the attribute maps nothing, and the generator reports PRAG2100 naming it.
Audiences
Section titled “Audiences”The NotificationAudience enum categorizes the intent of a notification:
| Audience | Description | Typical Channel |
|---|---|---|
EndUser |
Application customer (hotel guest, subscriber) | Email, Push, SMS |
Admin |
Platform operator, ops team | Email, Slack |
Developer |
DevOps, CI/CD systems | Webhook, Slack |
TenantAdmin |
Multi-tenant administrator | Email, InApp |
System |
Monitoring, health checks | Webhook |
The audience affects recipient resolution (the resolver may query different stores for different audiences) and can inform custom routing logic.
Channels
Section titled “Channels”NotificationChannel is a [Flags] enum with six values:
[Flags]public enum NotificationChannel{ None = 0, Email = 1 << 0, // SMTP via Pragmatic.Email Sms = 1 << 1, // SMS provider Push = 1 << 2, // Push notification InApp = 1 << 3, // In-application notification center Webhook = 1 << 4, // HTTP POST Slack = 1 << 5, // Slack webhook}Because it is a flags enum, routing can return Email | Push for High priority, and the pipeline will deliver to both channels independently.
Built-in Channels
Section titled “Built-in Channels”SmtpChannel (Pragmatic.Notifications): Delegates to Pragmatic.Email’s IEmailSender for SMTP delivery, connection pooling, DKIM, and middleware. Configuration splits sender identity (SmtpOptions) from transport settings (SmtpTransportOptions).
WebhookChannel (Pragmatic.Notifications.Webhook): Sends an HTTP POST with JSON payload. Uses a named HttpClient (Pragmatic.Notifications.Webhook). If NotificationContent.JsonPayload is set, it sends the raw payload; otherwise it serializes { Subject, Body, Timestamp } using AOT-safe JsonSerializerContext. A delivery that takes longer than WebhookOptions.Timeout (default 30 s, connection included) fails: n.AddWebhook(w => w.Timeout = TimeSpan.FromSeconds(5)). Signing, redelivery and secrets. Every request carries X-Pragmatic-Delivery, one id per delivery and the same on every retry of it, so a receiver de-duplicates on it. With SigningSecret set it also carries X-Pragmatic-Timestamp and X-Pragmatic-Signature: sha256=<hex> of HMAC-SHA256 over "{timestamp}.{deliveryId}.{body}" — the id is signed, so it can be trusted. A destination with a secret of its own gets it from a registered IWebhookSigningSecretResolver (asked with the URL and the tenant; null falls back to SigningSecret). WebhookOptions.ResiliencePolicy names a Pragmatic.Resilience policy (AddResiliencePolicy(name, p => p.Retry = new RetryOptions { … })) every delivery runs through: a non-success status or an exception is retried as it says, and Timeout bounds each attempt.
Custom Channels
Section titled “Custom Channels”Implement INotificationChannel and register via builder.AddChannel<T>():
public sealed class PushChannel : INotificationChannel{ public NotificationChannel Channel => NotificationChannel.Push;
public async Task<DeliveryResult> DeliverAsync( ResolvedRecipient recipient, NotificationContent content, CancellationToken ct) { // Deliver to push service return DeliveryResult.Succeeded(pushMessageId); }}Delivery Lifecycle
Section titled “Delivery Lifecycle”Pending --> Sent --> Delivered --> Read | | +---> Failed Bounced| Status | Meaning |
|---|---|
Pending |
Record created, delivery not yet attempted |
Sent |
Channel provider accepted the message |
Delivered |
Confirmed delivery (webhook: HTTP 2xx; email: accepted by relay) |
Read |
Recipient opened/read the notification (requires read-tracking integration) |
Failed |
Delivery attempt failed (error message stored) |
Bounced |
Email bounce or permanent delivery failure |
Background Processing
Section titled “Background Processing”EnqueueAsync writes to a BoundedChannel<NotificationRequest> (default capacity: 1000). A BackgroundService (NotificationDeliveryWorker) reads from this channel and processes through the same pipeline. This provides non-blocking fire-and-forget semantics with backpressure.
// Returns immediately with a tracking IDvar result = await notificationService.EnqueueAsync(request);// result.NotificationId is available for pollingThe worker catches exceptions per-request and logs them without crashing the background loop.
Multi-Tenant Support
Section titled “Multi-Tenant Support”The INotificationChannelFactory.GetChannel(channel, tenantId) signature supports per-tenant channel resolution. The DefaultChannelFactory ignores tenantId, but a custom factory can return different SMTP configurations per tenant.
NotificationRecord.TenantId is indexed in the EF Core store for efficient per-tenant queries.
Preferences Model
Section titled “Preferences Model”NotificationPreferences controls per-user delivery behavior:
Enabled– global kill switch; iffalse, no notifications are deliveredPreferredChannel– used when priority allows choice (Normal)MutedCategories– set of category strings the user has opted out ofDoNotDisturb– holds non-critical notifications (reserved for future digest support)Locale– for future localized template rendering
Implement INotificationPreferenceProvider to source preferences from your user profile database. The DefaultPreferenceProvider returns all-enabled defaults.
