Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
[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.
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"
});
The following example shows a formatter that takes a list of messages and joins their contents together.
[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 };
}
}
This example expands the last example with support for user notification formatting. In this example, it makes the content text blue.
public partial class StringJoinFormatter : IUserNotificationFormatter
{
public UserNotificationMessage FormatUserMessage(UserNotificationMessage notification)
{
notification.Content = "<div style=\"color:#0000FF\">" + notification.Content + "</div>";
return notification;
}
}
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;
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 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"; } }
}
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;
}
Last updated: May 17, 2016