Custom Notification In Liferay

Oct 23, 2021
2 mins

In this post, I am going to talk about notification feature and how to create custom notification in Liferay. Let’s start customization…

1. Create module in Liferay workspace

2. Create ‘SendNotificationToUserHandler’ Class

import org.osgi.service.component.annotations.Component;

import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.model.UserNotificationEvent;
import com.liferay.portal.kernel.notifications.BaseUserNotificationHandler;
import com.liferay.portal.kernel.notifications.UserNotificationHandler;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.StringUtil;

@Component(service = UserNotificationHandler.class)
public class SendNotificationToUserHandler extends BaseUserNotificationHandler{
	public static final String PORLET_ID = "com.xyz.news.items";
	
	public SendNotificationToUserHandler() {
		setPortletId(SendNotificationToUserHandler.PORLET_ID);
	}

	@Override
	protected String getBody(UserNotificationEvent userNotificationEvent, ServiceContext serviceContext)
			throws Exception {
		JSONObject jsonObject = JSONFactoryUtil.createJSONObject(userNotificationEvent.getPayload());
		String notificationTitle=jsonObject.getString("title");
		String notificationDetails = jsonObject.getString("status");
		String body = StringUtil.replace(getBodyTemplate(), 
				new String[] {"[$TITLE$]", "[$BODY_TEXT$]" },
				new String[] {"<strong>" + notificationTitle + "</strong>", notificationDetails });
		return body;
	}
	
	@Override
	protected String getLink(
		UserNotificationEvent userNotificationEvent,
		ServiceContext serviceContext) throws Exception {
		return super.getLink(userNotificationEvent, serviceContext);
	}

	@Override
	protected String getBodyTemplate() throws Exception {
		return "<div class=\"title\">[$TITLE$]</div><div>[$BODY_TEXT$]</div>";
	}
}

3. Create one method for save entry in database

JSONObject payload = JSONFactoryUtil.createJSONObject();
payload.put("status", "This text for notification description in brief." );
payload.put("title", "Notification Title");
payload.put("userId", "userId who send this notification");
private void sendNotification(User user, JSONObject payload) throws PortalException {
		SendNotificationToUserHandler hndlr = new SendNotificationToUserHandler();
		UserNotificationManagerUtil.addUserNotificationHandler(hndlr);
		
		if(UserNotificationManagerUtil.fetchUserNotificationDefinition(
				hndlr.getPortletId(),
				ClassNameLocalServiceUtil.getClassNameId(hndlr.getClass().getName()),
				UserNotificationDeliveryConstants.TYPE_WEBSITE) == null)
			{

			    UserNotificationDefinition def =
					new UserNotificationDefinition(
						hndlr.getPortletId(),
						ClassNameLocalServiceUtil.getClassNameId(
							hndlr.getClass().getName()
						),
						0,
						"Receive notification when this " +
						"portlet does something INCREDIBLE." );

			    def.addUserNotificationDeliveryType(
					new UserNotificationDeliveryType(
				    	"Website",
					    UserNotificationDeliveryConstants.TYPE_WEBSITE,
						true,
						true)
				);

				UserNotificationManagerUtil.addUserNotificationDefinition(
					hndlr.getPortletId(),
					def);
			}
		

		UserNotificationEvent notification =
			UserNotificationEventLocalServiceUtil.createUserNotificationEvent(
				CounterLocalServiceUtil.increment()
			);

		notification.setCompanyId(user.getCompanyId());
		notification.setUserId(user.getUserId());
		notification.setPayload(payload.toString());
		notification.setDeliveryType(UserNotificationDeliveryConstants.TYPE_WEBSITE);
		notification.setTimestamp(new Date().getTime());
		notification.setArchived(false);
		notification.setDelivered(true);
		notification.setType(hndlr.getPortletId());

		UserNotificationEventLocalServiceUtil.addUserNotificationEvent(notification);
	}

Comments

Loading...

Thanks for reading!

Here are some useful links before you go.