Saturday, December 6, 2014

Liferay Dockbar Custom User Notifications

Liferay 6.2 added new feature in dockbar. One of the feature is user notifications already well aware of dockbar in liferay. Liferay dockbar consist many options it will be appear once user login into the liferay and the options are based on user role.

The following screen shows Liferay Dockbar after user login into the portal


Assumption:

Assume portal administrator wants to send notification to all portal users when portal got new features.

This just example scenario we can make it better approach to send notifications but for understanding purpose i just took this assumption.

One of the Liferay Dockbar feature is User Notification display. This will be display user notifications and relevant messages with respect to users. If some document is added then we will notify to user about document is added. Some new blog post was added then we will notify to the user.

To Display User Notification in the Dockbar we need to deploy the Notifications Portlet then only we can see User Notifications in the dockbar.

Notification portlet available for Community Edition(Notifications CE) and Enterprise Edition(Notifications EE) in Liferay Market Place.

You must download portlet and deploy the portlet into your current Liferay Portal Instance. When you download make sure portlet version.

The following is Market Place Dowload URL (Notifications CE.lpkg)


You can also download from following URL



Once you downloaded and deploy the portlet, then you can see Notification Feature in the dockbar as like following screen.



Liferay have given flexibility to add custom notifications and we can show these notification in the Dockbar using Notification Portlet with respect to each login user.

Custom User Notification Implementation:

Liferay already have given some API classes and mechanism to implement custom notification and we can show in the dockbar. We need follow some set of steps to implement custom notifications.

In our portlet development we may get requirement to send notification to users now we will see how to implement custom user notifications.

Steps to Implement it in the Portlet Development
  1. Implement Custom User Notification Handler Class
  2. Define User Notification Definitions in XML file
  3. Configure User Notification Handler Class and User Notification Definitions Xml file in liferay-portlet.xml file
  4. Add user notifications using UserNotificationEventLocalServiceUtil
Implement Custom User Notification Handler Class

First we need to Implement Custom User Notification Handler class and it will take care of adding notifications in the dockbar. We implement logic like it will fetch respective user notifications and add html markup in the dockbar. Here we can design html to show the notification in the better way.

We should implement “UserNotificationHandler” interface for our Custom User Notification handler class.

Instead of directly implement “UserNotificationHandler” interface we can extends “BaseUserNotificationHandler” class so that we need not to implement all methods.

Do Design Html to add to dockbar we mainly implement the “getBody” and “getBodyTemplate” methods.

getBodyTemplate” We will design Notification Template and “getBody” we will fill the template with actual dynamic content.

The dynamic content will be getting from payload JSON object that will prepared at the time of user notification added in the database.

The example Implementation As follows


public class DockBarUserNotificationHandler extends BaseUserNotificationHandler {
public static final String PORTLET_ID = "dockbarnotificationaction_WAR_DockBarCustomNotificationportlet";

public DockBarUserNotificationHandler() {

setPortletId(DockBarUserNotificationHandler.PORTLET_ID);

}
@Override
protected String getBody(UserNotificationEvent userNotificationEvent,
ServiceContext serviceContext) throws Exception {
JSONObject jsonObject = JSONFactoryUtil
.createJSONObject(userNotificationEvent.getPayload());
//long userId = jsonObject.getLong("userId");
String notificationText=jsonObject.getString("notificationText");
String title = "<strong>Dockbar Custom User Notification for You</strong>";
String body = StringUtil.replace(getBodyTemplate(), new String[] {
"[$TITLE$]", "[$BODY_TEXT$]" },
new String[] { title, notificationText });
return body;
}
protected String getBodyTemplate() throws Exception {
StringBundler sb = new StringBundler(5);
sb.append("<div class=\"title\">[$TITLE$]</div><div ");
sb.append("class=\"body\">[$BODY_TEXT$]</div>");
return sb.toString();
}
}

Define UserNotification Definitions in XML file

This is very important step that we will define the notification information in the xml file. We have set of predefined tags to define notifications. We need to follows the “liferay-user-notification-definitions_6_2_0.dtd” for better understanding about notification tags. We have different type of Notification like Website and Email. Web Site type notification will be displayed in the dockbar.

We need to distinguish each notification by its Notification Type in our example we will distinguish by out custom PortetID.

The following is Example Notifications Configuration


<user-notification-definitions>
<definition>
<notification-type>${com.meera.notification.DockBarUserNotificationHandler.PORTLET_ID}</notification-type>
<description>receive-a-notification-when-administrator-triggered</description>
<delivery-type>
<name>email</name>
<type>${com.liferay.portal.model.UserNotificationDeliveryConstants.TYPE_EMAIL}</type>
<default>false</default>
<modifiable>true</modifiable>
</delivery-type>
<delivery-type>
<name>website</name>
<type>${com.liferay.portal.model.UserNotificationDeliveryConstants.TYPE_WEBSITE}</type>
<default>true</default>
<modifiable>true</modifiable>
</delivery-type>
</definition>
</user-notification-definitions>

For better understanding about notification configuration follow the micro-blogs portlet notification xml file


Configure Custom User Notification Handler Class and User Notification Definitions Xml file in “liferay-portlet.xml” file

Now we need to configure Custom User Notification Hander class and User Notification xml file in the our custom portlet “liferay-portlet.xml” file.
We will use two tags “<user-notification-definitions/>” and “<user-notification-handler-class/>”.We need to provide fully qualified path as value for the xml tags.

The following is configuration


<portlet>
<portlet-name>dock-bar-notification-action</portlet-name>
<icon>/icon.png</icon>
<user-notification-definitions>com/meera/notification/dockbar-user-notification-definitions.xml</user-notification-definitions>
<user-notification-handler-class>com.meera.notification.DockBarUserNotificationHandler</user-notification-handler-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>dock-bar-notification-action-portlet</css-class-wrapper>
</portlet>

Add user notifications using “UserNotificationEventLocalServiceUtil”

We have to add User Notification using UserNotificationEventLocalServiceUtil so that notification will be added in the database.

Where ever particular information should notify to the user then we will use UserNotificationEventLocalServiceUtil to add notification.

Here the main important is we have to create Payload JSON object and fill with required information and this information will be gathered in the Custom User Handler “getBody” class to prepare notification with dynamic content.

Generally we will add notification in the portlet action class or some other business logic writing area we may need to add notification.

The following is example code.


public void sendUserNotification(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
try {
List<User> users = UserLocalServiceUtil.getUsers(0,
UserLocalServiceUtil.getUsersCount());
ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);
String notificationText = ParamUtil.getString(actionRequest,"notifciationText");
for (User user : users) {
JSONObject payloadJSON = JSONFactoryUtil.createJSONObject();
payloadJSON.put("userId", user.getUserId());
payloadJSON.put("notificationText", notificationText);

UserNotificationEventLocalServiceUtil.addUserNotificationEvent(
user.getUserId(),DockBarUserNotificationHandler.PORTLET_ID,
(new Date()).getTime(), user.getUserId(),payloadJSON.toString(),false, serviceContext);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Notifications are added in the “UserNotificationEventtable.

Well done we have completed implementation part now we can deploy portlet can see the notifications.

Download Source Code and Portlet war file from below URL


Usage of Portlet

Download Notification Portlelt from market place or above specified URL and deploy into portal.

Download “DockBar Custom User Notification” from above URL there you can see “DockBarCustomNotification-portlet-6.2.0.1.war” file and Source code.

If you want use war file just place in liferay deploy directory then it will be deployed. From source also you can deploy it.

Now drag the portlet in the page,portlet name is “DockBar Custom User Notification” and available in “Sample” Category.

Once you add portlet to page you can see text area there enter some notification text then click on Notify button then notification will be send to all portal users.



Now notification is visible in each user dockbar.



Note:

This is not exact real time use case just for understanding purpose i have took the scenario and i am fetching all portal users, if your portal users are more then it take time to complete request so you can change logic in portlet cation class.

Reference Links:



Author

1 comment :

  1. It is right to said. I am never read like this article before. Your information is informative. With that, USA SEO Company service gives you best opportunity to rank your business.

    ReplyDelete

Recent Posts

Recent Posts Widget

Popular Posts