Home Liferay chat Openfire Chat Message Notification in Liferay
formats

Openfire Chat Message Notification in Liferay

Hi Liferay Experts,

Today there are many chat servers available in world. But robust and opensource will have greater rank among all of them. So today we are going to integrate Openfire, a widely used chat engine with Liferay at basic level.

Prerequisite

– Openfire chat server

– Liferay Plugin SDK

– Liferay runtime environment

How to setup Openfire with client

  1. Download openfire from here
  2. Execute the setup to install the openfire.
  3. Also have a look if needed @ Openfire Installation Guide

Now, Openfire chat server has been Installed and once server started we can access it at http://localhost:9090 with Admin console. Once you login in to the Admin console please create user for Chat server.

To test please download Spark, an Openfire client and login with created user and credentials. Find user and send them messages.

This way we can chat in Openfire with Spark.

 

Integration of Openfire in Liferay

  1. Create a portlet in Liferay (i.e. OpenChat-portlet)
  2. Create class OpenFireConnectionManager in portlet to connect with openfire server and get all messages
  3. Please connect with openfire by below code:

¬†ConnectionConfiguration connConfig = new ConnectionConfiguration(“localhost”, 5222);

connConfig.setSASLAuthenticationEnabled(false);
connConfig.setSendPresence(false);
XMPPConnection connection = new XMPPConnection(connConfig);
connection.connect();
connection.login(“username”, “password”); // User name and password of user whose message we want to get

4.  Please get offline messages by below code:

// Initialize the offline message manager
OfflineMessageManager offlineManager = new OfflineMessageManager(connection);

try{
// get offline messages
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager.getMessages();

// Print offline message count
System.out.println(“Number of offline messages:: ” + offlineManager.getMessageCount());

// get offline message
Map<String,ArrayList<Message>> offlineMsgs = new HashMap<String,ArrayList<Message>>();

// Iterate offline messages
while (it.hasNext()) {
org.jivesoftware.smack.packet.Message message = it.next();

// Get offline message sent date time
Date messageSentTime =((DelayInformation)message.getExtension(“x”,”jabber:x:delay”)).getStamp();

// Print offline message sent date time
System.out.println(messageSentTime);

// Print properties like from, messages body etc from offline message
System.out.println(“receive offline messages, the Received from [” + message.getFrom()+ “] the message:” + message.getBody());

// get sent user name of offline message
String fromUser = message.getFrom().split(“/”)[0];

// create offline message list
if(offlineMsgs.containsKey(fromUser)){
offlineMsgs.get(fromUser).add(message);
}else{
ArrayList<Message> temp = new ArrayList<Message>();
temp.add(message);
offlineMsgs.put(fromUser, temp);
}
}

// delete offline message after read, if we do not want to delete the offline message by code we just need to comment out below line.
offlineManager.deleteMessages();

}catch(Exception e){
log.error(“Error Message”,e);
}

From above code we able to get offline message for provided username and password.

Once we get offline message we can email or sms to the user. Like for email,

How to send chat notification email

String messageBody = ContentUtil.get(“/chat.tmpl”, true);

String messageSubject = “{CHAT_USER} sent you urgent message” // email subject

messageBody = StringUtil.replace(body, new String []{“[$PORTAL_USER$]”,”[$CHAT_USER$]”,”[$CHAT_TEXT$]”}, new String []{“{PORTAL_USER}”,”${OFFLINE_MESSAGE_SENDER}”,”{OFFLINE_MESSAGE_TEXT}”}); // replacing the body with our content.

MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toEmailAddress);
mailMessage.setFrom(fromEmailAddress);
mailMessage.setSubject(messageSubject);
mailMessage.setBody(body);
mailMessage.setHTMLFormat(true);

MailServiceUtil.sendEmail(mailMessage); // Sending message

Template file: (chat.tmpl)

Dear [$PORTAL_USER$],<br/><br/>

[$CHAT_USER$]. sent you message as below:<br/><br/>
[$CHAT_TEXT$]

Best,<br/>
Portal Admin

By above code we can send email notification if someone is sending any urgent chat message and we can help the organization people to be connected. Then choose here best Liferay Portal Development Companies.

Share!Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterFlattr the authorDigg thisPin on PinterestEmail this to someoneShare on StumbleUponShare on RedditShare on TumblrBuffer this pagePrint this page
Tags:
Copyright © 2017 - Scalsys. All Rights Reserved