HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

User notification examples

Shows how to work with user notifications and subscriptions for notifying users in Optimizely Content Management System (CMS).

User notifications

Create and post a message

The following example shows how to create and post a notification message.

var notifier = ServiceLocator.Current.GetInstance<INotifier>();
    INotificationUser receiver;
    INotificationUser sender;
    await notifier.PostNotificationAsync(new NotificationMessage()
        ChannelName = "epi.example",
        Content = "A page has been improved!",
        Subject = "Improvement",
        Recipients = new[] { receiver },
        Sender sender,
        TypeName = "PageChanged"

Create a custom formatter

The following example shows a formatter that takes a list of messages and joins their contents together.

    public class StringJoinFormatter : INotificationFormatter
        public string FormatterName { get { return "epi.stringjoinformatter"; } }
        public IEnumerable<string> SupportedChannelNames { get { return new[] { "epi.example" }; } }
        public Task<IEnumerable<FormatterNotificationMessage>> 
          FormatMessagesAsync(IEnumerable<FormatterNotificationMessage> notifications,
          string recipient,
          NotificationFormat format,
          string notificationChannelName)
            var message = new FormatterNotificationMessage(notifications.SelectMany(x => x.ContainedIDs)) 
                Subject = "Joined Message",
                Content = string.Join(Environment.NewLine, notifications)
            return Task.FromResult((IEnumerable<FormatterNotificationMessage>)new[] { message });

Add user notification formatting to a custom formatter

This example adds support for user notification formatting. In this example, it makes the content text blue.

    public class BlueFormatter : IUserNotificationFormatter
        public IEnumerable<string> SupportedChannelNames { get { return new[] { "epi.example" }; } }
        public Task<UserNotificationMessage> FormatUserMessageAsync(UserNotificationMessage notification)
            notification.Content = "<div style=\"color:#0000FF\">" + notification.Content + "</div>";
            return Task.FromResult(notification);

Retrieve user notifications

The following example shows how to retrieve the 25 latest unread notifications for a particular channel. It uses the formatter from the previous example; therefore, the content in the notifications will have blue text.

INotificationUser user;
    var repository= ServiceLocator.Current.GetInstance<IUserNotificationRepository>();
    var query = new UserNotificationsQuery
        ChannelName = "epi.example",
        User = user,
        Read = false
    var notifications = repository.GetUserNotificationsAsync(query, 0, 25, UserNotificationFormattingMode.Required).Result;

Create a custom provider

The following example shows a custom provider that outputs notifications to the console.

    public class PrintMessageProvider : INotificationProvider, INotificationProviderStatus
        public string ProviderName { get { return "PrintMessage"; } }
        public NotificationFormat GetProviderFormat() { return new NotificationFormat { MaxLength = 64, SupportsHtml = false }; }
        public Task 
          SendAsync(IEnumerable<ProviderNotificationMessage> messages, 
          Action<ProviderNotificationMessage> succeededAction, 
          Exception> failedAction)
            foreach (var item in messages)
            return Task.FromResult(true);
        public bool IsDisabled { get { return DateTime.Now < DateTime.Today.AddHours(6); } }
        public string DisabledReason { get { return "No printing after midnight"; } }


This example shows the usage of the subscription service.

public IEnumerable<INotificationUser> HandleContentSubscibers(ContentReference content, IList<INotificationUser> users, INotificationUser sender)
        // Make a subscriptionKey
        Uri subscriptionKey = new Uri(string.Format("epi://example/content/{0}", content.ID));
        // Subscribe all recipients of the notification, including the sender of the notification
        _subscriptionService.SubscribeAsync(subscriptionKey, users).Wait();
        _subscriptionService.SubscribeAsync(subscriptionKey, sender).Wait();
        // Get all subscribers except the sender of the notification
        var recipients = _subscriptionService.FindSubscribersAsync(subscriptionKey).Result;
        recipients = recipients.Where(u => !u.UserName.Equals(sender.UserName, StringComparison.OrdinalIgnoreCase));
        return recipients;