🧩 Framework Components
Notifications
📖 Docs

Notifications API - Optimizely CMS 12

Exam Area: Content Area 4 – Framework Components (15%)
Reference: https://docs.developers.optimizely.com/content-management-system/docs/using-notifications


1. User Notifications

The CMS has a User Notifications system for sending notifications to users directly within the CMS UI (bell icon).


2. INotificationRepository

using EPiServer.Notification;

public class NotificationService
{
    private readonly INotificationRepository _notificationRepository;

    public NotificationService(INotificationRepository notificationRepository)
    {
        _notificationRepository = notificationRepository;
    }

    // Send a notification
    public async Task SendNotificationAsync(string recipientUsername, string message)
    {
        var notification = new NotificationMessage
        {
            ChannelName = "epi.cms.contentediting.notifications",
            TypeName = "contentapproval",
            Sender = new NotificationUser { UserName = "system" },
            Recipient = new NotificationUser { UserName = recipientUsername },
            Content = message,
            // Optional: link
            SendEmail = false,
            SendOnChannel = true
        };
        
        await _notificationRepository.CreateAsync(notification);
    }
}

3. Notification Formatter

// Format a notification for display in the UI
[ServiceConfiguration(typeof(INotificationFormatter))]
public class MyNotificationFormatter : INotificationFormatter
{
    public string ChannelName => "my.custom.channel";

    public NotificationMessage FormatMessage(
        NotificationMessage message, string recipient)
    {
        // Customize the notification message
        message.Content = $"[MY APP] {message.Content}";
        return message;
    }
}

4. Content Approval Notifications

The CMS automatically sends notifications when:

// Configure email notifications for content approvals
// Admin → Config → Content Approvals → Email Settings

5. Subscription Keys

// Subscribe to specific notification types
public class NotificationSubscriber
{
    private readonly INotificationRepository _repo;

    public async Task SubscribeToApprovals(string username)
    {
        var subscription = new NotificationSubscription
        {
            UserName = username,
            Enabled = true
        };
        
        await _repo.SubscribeAsync(subscription, 
            "epi.cms.contentediting.notifications");
    }
}

6. Notification Examples

// In a workflow: Send a notification when content needs review
[InitializableModule]
public class WorkflowNotificationModule : IInitializableModule
{
    private IContentEvents _contentEvents;
    private INotificationRepository _notificationRepo;

    public void Initialize(InitializationEngine context)
    {
        _contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();
        _notificationRepo = context.Locate.Advanced.GetInstance<INotificationRepository>();
        
        _contentEvents.RequestingApproval += OnRequestingApproval;
    }

    private async void OnRequestingApproval(object sender, ContentEventArgs e)
    {
        var notification = new NotificationMessage
        {
            ChannelName = "epi.cms.contentediting.notifications",
            TypeName = "contentapproval",
            Sender = new NotificationUser { UserName = e.RequiredRole ?? "system" },
            Recipient = new NotificationUser { UserName = "approver@example.com" },
            Content = $"Content '{e.Content?.Name}' needs approval",
            SendEmail = true,
            SendOnChannel = true
        };
        
        await _notificationRepo.CreateAsync(notification);
    }

    public void Uninitialize(InitializationEngine context)
    {
        _contentEvents.RequestingApproval -= OnRequestingApproval;
    }
}

7. Email Notifications

// appsettings.json - Email configuration
{
  "EPiServer": {
    "Framework": {
      "EmailSettings": {
        "Host": "smtp.example.com",
        "Port": 587,
        "EnableSsl": true,
        "UserName": "cms@example.com",
        "Password": "secret",
        "From": "CMS System <cms@example.com>"
      }
    }
  }
}

Review Questions

  1. Which interface is used to send notifications? (INotificationRepository)
  2. Where do notifications appear in the CMS? (Bell icon in the navigation bar)
  3. What do SendEmail and SendOnChannel do in NotificationMessage?
    • SendEmail: Sends an email notification
    • SendOnChannel: Displays the notification in the CMS UI
  4. How do you subscribe to receive notifications? (INotificationRepository.SubscribeAsync())
  5. When does the CMS automatically send notifications? (Content approval workflow)