5 min read

How to send a Notification

To send a notification, you will need to create a workflow with the desired trigger, then fetch or specify the recipient(s) and compose the notification.

Directus has the ability to notify users through Flows. This will appear to the user as a red dot near the bell icon with the number of unread messages and also be sent via email. This article will show you how to configure Flows to send notifications.

To send a notification, you will need to create a workflow in Flows with the desired trigger, then fetch or specify the recipient(s) and compose the notification. When this workflow is triggered, the notification will be sent to the recipient.

Notifications can be a powerful communication tool or become a constant annoyance to the recipiants. So the first question to ask is Do I really need this?  and if so, When is the best time to send this?

Flows in Directus can help. You can introduce intelligent routing to send notifications at important points in your workflow and ensure the message is sent at the right moment. This is where the status field comes in handy.

A status field can hold as many states as you require and is the perfect parameter to use to target notifications. Let's create a basic notification in Flows using the status field to route notifications.

Step 1: Recipients

To send notifications, you will need to have users in Directus. First make sure to create user accounts and note down their IDs. This can be seen in the URL when viewing their profile and will look something like this:

a12bc3d-e4fg-5678-9012-34h5678ij90k

Another way is to send the notification to all the users that are assigned to a role in Directus. For this, you will need the role ID. To get the role ID, open Settings by clicking the Cog icon, then go to Roles & Permissions. Click on the role and copy the role ID from the URL. This will be formatted the same as the User ID above.

Step 2: Create a Trigger

To begin, click on the cog icon to open the settings, then click Flows. Create a new flow to open the trigger options window.

Name the flow accordingly and move onto the Trigger Setup. I'm wanting to trigger my workflow from within Directus so I'll use an Event Hook.

Example of setting up an Event Hook Trigger

I've chosen to set the scope to items.create and items.update to capture all events when something is created or updated in my collection.

Now I have my canvas for creating my workflow.

Empty canvas for my flow

Step 3: Filters

Next, use a filter for the incoming events to bring context to the notification. For this example, I'm wanting to notify my recipients when a record is requesting approval. I will use a status that I created called "Request Approval".

Click the (+) and choose Condition. Give it a memorable name and create a filter on the $trigger.payload.status.

Example of a condition that filters the status field

In the example above I've added a filter on the status value. This follows the same filter rules from the documentation. The absense of the status field or a status that is not "Request Approval" will follow the false route in this example. If needed, you can build actions in the false route as well. This is the code to use in the Condition Rules.

{
    "$trigger": {
        "payload": {
            "status": {
                "_eq": "Request Approval"
            }
        }
    }
}

The $trigger parameter contains the payload, collection and key as the item ID (or keys as an array list when triggered by items.update). The payload will only contain the fields submitted when saving the record.

💡
Default field values are not included in the payload. You can only filter on the fields and values that are changed or input before submission. If you do need information from the record, you can Fetch Data using the collection and key/keys first, then perform the Condition.
Example of the condition in place

Now the condition is in place. You'll notice there is now a Tick and Cross attached to this card. The tick is the positive/true response route and the cross is the negative/false route.

Step 4: Send the Notification

Click the tick to create a new step in the workflow. Paste the user ID of the recpient and a title for the notification.

💡
Make sure to press enter after pasting the User ID in the User field
Example of the notification dialog

You can add multiple recipients by copy and pasting more user IDs and pressing enter. They will appear in a list under the field.

Example of multiple recipients

Now our workflow is looking more complete.

Example of the flow

Step 5: Testing and Troubleshooting

Now that the workflow is in place, try performing the action that triggers the flow. In this case, I will change an article status to 'Request Approval'. If nothing happens, then you can check the workflow's log. Open the flow and look into the right-hand side bar. There is a section labelled Logs. It might take a minute or so to appear. Click the time point that matches your action.

Example of a log in the data flow

The log is grouped into each step with a colored bullet point to indicate if it was successful. Purple is successful, red (or pink) is a failed step. In the screenshot below, I've expanded the payload to reveal the API response. In this case, the recipient field was left empty blank.

Example of a failed data flow

If the notification is successful, the recipient will get a little red notification in the bottom corner of their screen.

Example of a notification in Directus

Clicking on the bell will open the notification inbox.

Example of the notification inbox

An email is also sent to the recipient's email address where the notification Title is the email subject and the notification Message is within the email body using a nice email template. Here is an example of the email template. The logo at the top will be your registed logo in Directus and the sign off message at the bottom will use your project name.

Example of the email sent to the recipient

Conclusion

In this article I have demonstrated how to create a workflow using Flows to send notifications to your Directus users. This can be repeated for any other collections or you can include multiple collections in your trigger if relevant. There is the temptation to expand the data flow to include more information such as links, names, content etc. This is possible but in my experience, this can cause more issues than it's worth. Simplicity is best.

By continuing to use our website, you consent to use essential cookies. We also use optional tracking cookies which help us gather statistics to improve our services. Do you consent to these cookies?

I Consent Do not track