Display Toast Notifications for Server Side Events with Lightning Web Components

NOTE: The project discussed in this post is on GitHub.

I recently needed to display a message to users in response to a server side event. Sometimes the message would only need to be displayed for the user who generated the event. For example, a user saves a record and something happens in a trigger they need to be notified of. We also had a use case where we want to notify every user who is currently using a particular application. For example, when a long running process completes.

I created a platform event to deliver the server side events. Then I created a lightning web component to receive the events and display a toast notification. The platform event is called Client Notification and it has five fields. Three fields set content of the notification:

  • Title: The notification title
  • Message: The notification message
  • Type: The type of notification.

The type can be SUCCESS, INFO, WARNING OR ERROR.

Two fields determine who sees the notification:

  • Application: Deliver the notification to a specific app or leave blank for all apps.
  • User: Deliver the notification to a specific user or leave blank for all users.

Add the Platform Event Notifier to a Page

The lightning web component is called Platform Event Notifier. In the lightning app builder it will appear in the list of custom components. You can add it to any page you want to receive notifications.

Click on the component in the app builder to access its properties. Enter an application name if you want to filter notifications by application. Otherwise, leave it blank.

Then save the page and activate, if necessary.

Sending Notifications

To send a notification publish a Client Notification platform event. I created a ClientNotification class with a method called SendNotification to simplify this.

Signature:

public static void SendNotification(String application, 
String title, 
String message, 
ClientNotificationType notificationType, 
Boolean allUsers);

view raw SendNotification.cls hosted with ❤ by GitHub

Parameters:
application
Type: String
The name of the application to notify. Null for all applications.

title
Type: String
The title of the notification

message
Type: String
The message displayed in the notification

notificationType
Type: ClientNotificationType
The type of notification to display. Can be ERROR, INFO, WARNING or ERROR.

allUsers
Type: Boolean
True if the notification should be displayed for all users. False if it should only be displayed for the user who generated the server side event.

Example 1

Display a notification only to the user whose action caused the event but don’t filter by application. For example, notify the user of related records updated during an update trigger.

Example 2

Display a notification for all users of a specific application. For example, notify users in real-time when a long running process completes.

I hope that’s useful.

Leave a Comment