# Kanject.NotificationHub

A multi-channel notification engine — send a ready-made message on a single channel, or publish an event and let the hub resolve templates, channels, scheduling, and cancellation. Eight channels: SMS, email, web, push, in-app, WhatsApp, and webhook.

> **Note:** Developer guide. For the trigger-mode tour and the bring-your-own-provider matrix, see the [NotificationHub product page](https://www.kanject.com/baas/notifications/).

**You'll learn**

- Provision NotificationHub into your AWS account
- Register it with `AddNotificationHubServer` and inject `IHubIngestionManager`
- Send a content notification and publish an event notification
- Understand the channel model, dispatch priority, and bring-your-own providers

## Provision

```bash
kanject baas deploy notifications --env dev
```

## Register it in your service

Register with `AddNotificationHubServer(options => …)` from `Kanject.NotificationHub.Provider.AwsV2.Extensions`, passing a typed `AwsHubConfiguration` delegate (region, namespace, credentials). A generic `AddNotificationHubServer<THubDbContext>` overload lets you supply your own data context.

```csharp
using Kanject.NotificationHub.Provider.AwsV2.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNotificationHubServer(options =>
{
    options.Region    = appSettings.AwsRegion;
    options.Namespace = appSettings.Stage;   // per-stage isolation
});

var app = builder.Build();
app.Run();
```

## Send a notification

Inject `IHubIngestionManager`. There are two entry points: `IngestContentNotificationAsync` sends a message you've already rendered on **one** `PublishChannel`; `IngestEventNotificationAsync` publishes an event topic + payload and lets the hub resolve templates, channels, and any scheduling on its side.

```csharp
// Inject IHubIngestionManager — the send surface has two entry points.
public class OrderNotifier(IHubIngestionManager hub)
{
    // Content notification: you supply the rendered message + one channel.
    public Task ShippedAsync(string toEmail) =>
        hub.IngestContentNotificationAsync(new IngestContentNotificationRequest
        {
            To             = toEmail,
            Subject        = "Your order shipped",
            Body           = "It's on the way — arriving tomorrow.",
            Channel        = PublishChannel.Email,
            DispatchPriority = NotificationDispatchPriority.Transactional,
        });

    // Event notification: publish an event topic + payload; templates and
    // channels resolve on the hub side (including scheduling/cancellation).
    public Task OrderPlacedAsync(JsonDocument payload) =>
        hub.IngestEventNotificationAsync(new IngestNotificationRequest
        {
            EventTopic = "order.placed",
            Payload    = payload,
        });
}
```

> **Pitfall:** `PublishChannel` is a single value per content send, **not** a `[Flags]` bitmask — `Email | Push` does not mean "both". To hit multiple channels, send per channel, or drive an event notification whose template targets several.

## Scheduling & cancellation

- **Delayed / recurring / calendar** — scheduled sends via `NotificationScheduleType` (`Delayed`, `Recurring`, `Calendar`).
- **Smart cancellation** — a `ScheduleCancellationEventDto { EventTopic, CorrelationId }` drops a queued send when a correlated event arrives.
- **Dispatch priority** — `NotificationDispatchPriority` routes transactional sends through a high-priority queue ahead of bulk.

## What you get

- **Eight channels** — `PublishChannel`: SMS, Email, Web, Push, InApp, WhatsApp, Webhook (single-select per content send).
- **Two send modes** — pre-rendered content sends, or event notifications the hub templates and routes.
- **Scheduling** — delayed, recurring, and calendar sends, with correlated-event cancellation.
- **Dispatch priority** — separate high- and low-priority outgoing queues.
- **Bring your own delivery** — implement `IHubDispatchProvider` and mark it `[NotificationPublisherProvider]` to add a channel/provider.

**Recap**

- Provision with `kanject baas deploy notifications`, register with `AddNotificationHubServer(options => …)`.
- Send via `IHubIngestionManager` — `IngestContentNotificationAsync` (one channel) or `IngestEventNotificationAsync` (topic + payload).
- `PublishChannel` is single-select (8 channels), not a flags bitmask; scheduling/cancellation ride event notifications.
- Add delivery providers by implementing `IHubDispatchProvider` + `[NotificationPublisherProvider]`.

## Related modules

- **[EventHub](https://www.kanject.com/docs/baas-eventhub/)** — The event source that triggers sends.
- **[Identity](https://www.kanject.com/docs/baas-identity/)** — User contact info and channel opt-ins.
- **[InstantMessaging](https://www.kanject.com/docs/baas-im/)** — In-app delivery and push-on-offline.

> **Next step:** Driving sends off domain events? Pair NotificationHub with [EventHub](https://www.kanject.com/docs/baas-eventhub/), or read the [BaaS deployment guide](https://www.kanject.com/docs/baas/) for module composition.

---
_Source: https://www.kanject.com/docs/baas-notifications/ · Kanject Docs_
