We have looked at the way Notifications are handled in Umbraco 9 and how they differ from Umbraco 8 counterpart; Events. But did you know you can create, send and handle your own custom Notifications within your Umbraco 9 application? Let's demonstrate!
Creating a Notification
Umbraco ships with a variety of Notifications we can hook into, but what if we want to use the existing infrastructure to send a custom notification throughout our application? Well, we can do that, and it's surprisingly easy! For our custom Notification to be publishable, all we have to do is create a class that implements the (empty) interface INotification, and the rest is up to us!
Let's create a small example in which we want to send out a notification whenever we press a button. Easy enough! We will create a new class called WePushedAButtonNotification, and have it implement the INotification interface. For demonstration purpose, let's also give it a property TimeOfPushing, so that we can use that throughout our NotificationHandlers! We have now created a Notification that's ready for sending!
Sending a Notification
To send our Notification throughout our application, we have two options:
- IEventAggregator, which will always send out our Notification immediately when called.
- IScope.Notifications, which will send out our Notification once the scope has been completed and disposed.
For this example, we'll take a look at the IEventAggregator. For more information on the differences between the two versions, take a look at the Umbraco Docs. We will need to find a way to trigger our Notification, which in our example will be an UmbracoApiController endpoint that we can submit a HttpRequest to! Within this controller, we have to inject an instance of IEventAggregator, which gives us the option to call the .Publish() method to send out our Notification! Within the .Publish() method we instantiate a new instance of our custom Notification class, where in this example we also pass in the current DateTime to the constructor of the WePushedAButtonNotification!
We wrap this neatly within an IActionResult returning, HttpPost decorated, easily route-able endpoint, and we now have the option to send out our custom Notification throughout our application!
Handling a Notification
Because we stuck with the same conventions as used by Umbraco itself, the way we handle our Notifications isn't all that different as the rest of the Notifications implemented by default! We start off by creating a new class that implements the INotificationHandler<T> interface, where in our case T is the WePushedAButtonNotification. When we implement the interface, we automatically get our instance of the Notification available within the Handle method, that's get called whenever this notification gets send!
From here, we have complete freedom to do whatever we like with the Notification. Perhaps we want to Dependency Inject one or more Umbraco services to read/write Content or Media? Perhaps do something with an external database? You can do whatever you want! Let's keep it simple for demonstration purpose, and just stick with a simple Debug.WriteLine() so that we can test that our notification gets called whenever we send out a notification!
Don't forget that we still have to register our NotificationHandler for our Notification to be handled! We do this by registering our NotificationHandler from within the ConfigureServices method in our Startup.cs, where the first parameter TNotification is the Notification we wish to handle, and the second parameter TNotificationHandler is the NotificationHandler we wish to register to this Notification.
And there we have it! We have successfully created, sent and handled our custom Notification within our Umbraco 9 application! Now each time our API endpoint gets called, a notification gets send throughout our application, and all registered NotificationHandlers are ready to receive and handle it!
Using the existing Umbraco 9 infrastructure, it is shockingly easy to create, send and handle a custom Notification. By implementing the INotification interface to create our notification, using the IEventAggregator to send said notification, and using the INotificationHandler interface to handle (and don't forget register!) our Notification Handler, we're good to go! 🔥
If you have any further questions, feel free to contact me over at my socials available at the Contact page, and I'd love to hear about the amazing creations you're able to come up with! 😄
Sources used in this article: