Learn Liferay DXP custom notification in 5 minute

admin

April 27, 2022

Welcome to EnProwess blogs. In this post, I am going to talk about notification feature and how to create custom notification in DXP. Before we talk about notification feature in DXP, lets understand what has changed in DXP with compare to Liferay 6.2.

In Liferay 6.2,

  • There is Notification display portlet. This portlet can be used to display notification to all or specific user in portal.
  • Notification display portlet is also embedder in docbar to display number of notification waiting for user.
  • XML configuration required to define custom notifications class, notification definition etc.

In DXP,

Now let’s understand how to define custom notification in DXP. Before you start further, ensure you have following prerequisite installed/configured

Following are the steps to implement custom notification.


  1. Create module in Liferay workspace:

To create Liferay modules, Click File menu → Click New → Select Liferay Module Project. Provide appropriate name for module and click finish.


  1. Implement Custom User Notification Handler Class:

  • Create SendNotificationToUserHandler class and extends BaseUserNotificationHandler abstract class.
  • BaseUserNotificationHandler implements UserNotificationHandler interface so by extending this abstract class you will not be forced to implement all methods of UserNotificationHandler
  • UserNotificationHandler‘s job is to interpret notification events and determine to whom to deliver it. For ex. To which user and site etc.


    2.1. Now implement @Component block in SendNotificationToUserHandler and use “UserNotificationHandler” service which is registered as an OSGI Service as follow,

    Implement Custom User Notification Handler Class

    2.2. Implement getBody() and getBodyTemplete() methods as follow in SendNotificationToUserHandler class,

     Implement getBody() and getBodyTemplete() methods

  1. Add notifications information in database table:

You have successfully implemented custom notification class. Now its time to make notification entry in DB. Generally, this entry would be done from custom portlet based on business requirement to notify user about different events. Just to demonstrate how it works, I am adding one dummy entry in usernotificationevent table.

  • You need to add Notification in usernotificationevent table using “UserNotificationLocalServie” service. In DXP, you need to use XXXLocalService class instead of XXXLocalServiceUtil for DB operation. You may read more about it from following link, https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/finding-and-invoking-liferay-services
  • Refer following code block to understand how to make database entry for notification. You can also manually insert record in table for demo purpose,

Add notifications information in database table

With above steps, you have successfully implemented custom notification and made database table entry to display notification. It’s time to deploy code to see how it looks like. In my case, this custom notification is being rendered as following snapshot,

  • You will get notification intimation as follow,

    get notification intimation
  • You can see complete notification message as follow on click of user icon,

    notification message

I hope this post gives idea about difference between Liferay6.2 and DXP to implement custom notification. EnProwess believed in learning and sharing. You can read different implementation approaches for DXP on our site. Keep learning!

Post by,

Zeenesh Patel

Subscribe
Notify of
guest
19 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments