Umbraco 9: Content Publish Notifications

16/10/2020 umbraco umbraco9

This article will discuss Umbraco publish notifications aswell as show how to add these to our solution using IUmbracoBuilder. We'll build an on publish notification to send a message to a discord server.

This is an updated version of this post for v8.

Setup 🔧

Firstly, we need to create the webhook in discord (but wherever you want to post to will have a place to create a webhook, like slack, etc). In Discord, you can right click on channel and edit, you can add a webhook here (if you have permissions!):

I always go to Postman first, and test the webhook works before I try to then use it in code. I do this whenever I integrate with a new API.

and here we see it in our Discord channel:

So, it's all working fine directly, now we need to hook this up to an Umbraco notification that is called on publish of a node. You can read more about that here. I'd recommend reading this before continuing as this explains the concepts we'll use.

Let's get coding! 👩‍💻

This is built on a new Umbraco 9 project that has a doctype with related content created. This can be anything, it's so we have something to publish in our example later. See docs on getting started with Umbraco 9 here.

I want my notification to occur on content publish so will use "ContentPublishingNotification", see example below. This posts a message to our discord when we publish, you can see this POST to discord webhook in the Handle method which is called on the publish of the content.

To see more examples of notifications scenarios, check out these examples by Umbraco Docs team.

We need to add a IUmbracoBuilder to tell our application to use our DiscordNotification when content is published. This uses Notification Handlers, in this example ContentPublishingNotification:

Finally, we need to add our handler to the services on start up. We can edit the ConfigureServices code in our Startup.cs:

It works! 🎉

Now, when we publish a node in Umbraco, we get this wee message in Discord! This may not be super useful to notify with every publish, but we could make this only be specific to certain page types or user groups, for example.