Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Usage examples [Beta]

[New in Episerver CMS Core 9.4]

Note: This is a pre-release API that is UNSTABLE and might not satisfy the compatibility requirements as denoted by its associated normal version.

User notifications

Creating and posting a message

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

C#
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"
});

Creating a custom formatter

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

C#
[ServiceConfiguration(typeof(INotificationFormatter))]
public partial class StringJoinFormatter : INotificationFormatter
{
        public string FormatterName { get { return "epi.stringjoinformatter"; } }

        public IEnumerable<string> SupportedChannelNames { get { return new[] { "epi.example" }; } }

        public IEnumerable<FormatterNotificationMessage> FormatMessages(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 new[] { message };
        }
}

Add user notification formatting to a custom formatter

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

C#
public partial class StringJoinFormatter : IUserNotificationFormatter
{
    public UserNotificationMessage FormatUserMessage(UserNotificationMessage notification)
    {
        notification.Content = "<div style=\"color:#0000FF\">" + notification.Content + "</div>";
        return notification;
    }
}

Retrieving 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.

C#
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;

Creating a custom provider

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

C#
[ServiceConfiguration(typeof(INotificationProvider))]
public class PrintMessageProvider : INotificationProvider, INotificationProviderStatus
{
    public string ProviderName { get { return "PrintMessage"; } }

    public NotificationFormat GetProviderFormat() { return new NotificationFormat { MaxLength = 64, SupportsHtml = false }; }

    public void Send(IEnumerable<ProviderNotificationMessage> messages, Action<ProviderNotificationMessage> succeededAction, Action<ProviderNotificationMessage, Exception> failedAction)
    {
        foreach (var item in messages)
            Console.WriteLine(item.Content);
    }

    public bool IsDisabled { get { return DateTime.Now < DateTime.Today.AddHours(6); } }

    public string DisabledReason { get { return "No printing after midnight"; } }
}

Subscriptions

C#
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;

}
Do you find this information helpful? Please log in to provide feedback.

Last updated: May 17, 2016

Recommended reading