Are you dreaming of getting a push notification when your Publish Transaction has been finished, just like in the image below?
Check out this extension and read on further to discover how I did this.
Using the notification framework introduced in SDL Web 8.5
SDL Web 8.5 introduced a new feature to send out notifications to all connected browsers running Content Manager Explorer.
This can be used to target users or groups and show them a Message Center notification, eg. that a workflow task has been assigned to them. It can also be used for more technical solutions, for instance to update information in a view when someone updated a component you are viewing in Experience Manager.
Notifications can be sent from any system, and are therefore not restricted to be sent from Content Manager event handlers. The framework supports load balanced / scaled out setups.
Do note, however, that the framework sends notifications to all clients, so do not send sensitive information out with a notification. Also make sure that your UI event handler decides whether to process the notification or not based on the Action in the notification message.
The framework does not persist notifications, they will only be sent out to clients connected at that time.
So how do you send a notification?
In this example I am creating a message in a CM eventhandler. In the TOM.NET Session object a new NotificationsManager has been added, that exposes a BroadcastNotification method. This API can be used to send a NotificationMessage object.
First of all, in your eventhandler, you need a reference to the Tridion.ContentManager.dll assembly. Then add a using statement for the namespace Tridion.ContentManager.Notifications. The next code snippet shows how to create and send the message:
private static void SendNotification(PublishTransaction subject, SaveEventArgs e, EventPhases phases)
{ NotificationMessage message = new NotificationMessage
{
// we need an identifier that we can use in the UI extension to distinguish our messages from others
Action = "example:publishtransactionfinished",
SubjectIds = new[] { subject.Id.ToString() },
Details = "{ 'myProperty': 'value' }"
};
subject.Session.NotificationsManager.BroadcastNotification(message);
}
Do make sure to prefix your Action, similar to xml namespaces, to ensure uniqueness. I'd suggest to use the company name or acronym, eg "sdl:myaction".
As mentioned earlier, this notification can also be broadcast outside of CM event handlers, through using the API on the CoreService:
client.BroadcastNotification(message);
In this example client is an instance of either CoreServiceClient or SessionAwareCoreServiceClient.
Okay and now how do I consume this notification in the UI?
Well you need a reference to an instance of Tridion.Web.UI.Core.NotificationBroadcaster in your UI extension code. Add an event listener for your UI handler that processes the message like this (notificationHandler is the function that processes the message):
var notificationHandler = function(event)
{}
var notificationBroadcaster = Tridion.Web.UI.Core.NotificationBroadcaster.getInstance();
notificationBroadcaster.addEventListener("notification", notificationHandler);
Do make sure to check for the right Action before processing the notification, and in case you want to target specific users or groups, also check this before continuing to process the notification:
var notificationHandler = function(event)
{
// only proceed if the message is the message we broadcasted ourself from the CM eventhandler
if (event.data.action !== "example:publishtransactionfinished")
{
return;
}
var userSettings = Tridion.ContentManager.UserSettings.getInstance();
// only process the message when it is meant for this user.
if (event.data.details.creatorId === userSettings.getUserId())
{
}
}
The example extension: Publish Transaction Notification
I have built an extension as a showcase on how to use the Notification Framework. It shows how to create a CM eventhandler to broadcast a message when a Publish Transaction is finished using the Framework and an UI model extension to respond to the broadcasted message to show a notification with the status.
You can find the complete source code for this end to end example here: https://github.com/sdl/publish-transaction-notification
See also the official documentation topics:
How to consume a notification in a UI event handler
Notification broadcasting CM Event Handler example