Hide menu Last updated: Jun 14 2017
Area: Episerver CMS Applies to versions: 10.10 and higher
Other versions:

Usage examples

User notifications

Creating and posting 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"
});

Creating a custom formatter

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

[ServiceConfiguration(typeof(INotificationFormatter))]
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.

[ServiceConfiguration(typeof(IUserNotificationFormatter))]
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); } }

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.

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.

[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 Task SendAsync(IEnumerable<ProviderNotificationMessage> messages, Action<ProviderNotificationMessage> succeededAction, Action<ProviderNotificationMessage, Exception> failedAction)
    {
        foreach (var item in messages)
            Console.WriteLine(item.Content);

        return Task.FromResult(true);
    }

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

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

Subscriptions

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;

}

Comments