Messaging Essentials

This page documents the details of working with using the Messaging component to include a messaging feature on a website.

Essentials for Client-Side

Compose Message

resourceType

social/messaging/components/hbs/composemessage

clientllibs

cq.social.hbs.messaging

templates /libs/social/messaging/components/hbs/composemessage/composemessage.hbs
css /libs/social/messaging/components/hbs/composemessage/clientlibs/composemessage.css
properties See Configure Messaging
admin configuration Configure Messaging

Message List

(for Inbox, Sent, and Trash)

resourceType

social/messaging/components/hbs/messagebox

clientllibs

cq.social.hbs.messaging

templates /libs/social/messaging/components/hbs/messagebox/messagebox.hbs
css /libs/social/messaging/components/hbs/messagebox/clientlibs/messagebox.css
properties See Configure Messaging
admin configuration Configure Messaging

See also Client-side Customizations

Essentials for Server-Side

CAUTION

The String parameter must not contain a trailing slash “/” for the following MessageBuilder methods:

  • setInboxPath()
  • setSentItemsPath()

For example:

valid: mb.setInboxPath( "/mail/inbox" );
 not valid: mb.setInboxPath( "/mail/inbox/" );

Community Site

A community site structure, created using the wizard, includes the messaging feature when selected. See User Management settings of Community Sites Console.

Sample Code: Message Received Notification

The Social Messaging feature throws events for operations, for example send, marking read, marking delete. These events can be caught and actions taken on the data contained in the event.

The following example is of an event handler which listens for the message sent event and sends an email to all message recipients using the Day CQ Mail Service.

To try the server-side sample script, you need a development environment and the ability to build an OSGi bundle:

  1. Log in as an administrator to [CRXDE|Lite](https://localhost:4502/crx/de).

  2. Create a bundle nodein /apps/engage/install with arbitrary names, such as:

    • Symbolic Name: com.engage.media.social.messaging.MessagingNotification
    • Name: Getting Started Tutorial Message Notification
    • Description: A sample service for sending an email notification to users when they receive a message
    • Package: com.engage.media.social.messaging.notification
  3. Navigate to /apps/engage/install/com.engage.media.social.messaging.MessagingNotification/src/main/java/com/engage/media/social/messaging/notification, and then:

    1. Delete the Activator.java class automatically created.
    2. Create class MessageEventHandler.java.
    3. Copy and paste the code below into MessageEventHandler.java.
  4. Click Save All.

  5. Navigate to /apps/engage/install/com.engage.media.social.messaging.MessagingNotification/com.engage.media.social.messaging.MessagingNotification.bnd, and add all the import statements as written in the MessageEventHandler.java code.

  6. Build the bundle.

  7. Ensure Day CQ Mail ServiceOSGi service is configured.

  8. Log in as a demo user, and send email to another user.

  9. The recipient receives an email regarding a new message.

MessageEventHandler.java

package com.engage.media.social.messaging.notification;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.HtmlEmail;
import java.util.List;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.social.messaging.api.Message;
import com.adobe.cq.social.messaging.api.MessagingEvent;
import com.day.cq.mailer.MessageGatewayService;
import com.day.cq.mailer.MessageGateway;

@Component(immediate=true)
@Service(EventHandler.class)
@Properties({
        @Property(name = "event.topics", value = "com/adobe/cq/social/message")
})
public class MessagingEventHandler implements EventHandler {
    private Logger logger = LoggerFactory.getLogger(MessagingEventHandler.class);

    @Reference
    ResourceResolverFactory resourceResolverFactory;

    @Reference
    private MessageGatewayService messageGatewayService;

    ResourceResolver resourceResolver=null;
    MessageGateway messageGateway=null;

    public void sendMail(String from, String to,String subject, String content){
        Email email = new SimpleEmail();
        messageGateway = messageGatewayService.getGateway(SimpleEmail.class);
        try {
         email.addTo(to);
            email.addReplyTo(from);
            email.setFrom(from);
            email.setMsg(content);
            email.setSubject(subject);
         messageGateway.send(email);
        } catch(EmailException ex) {
            logger.error("MessageNotificaiton : Error sending email : "+ex.getMessage());
        }
        logger.info("**** MessageNotification **** Mail sent to " + to);
    }

    public void handleEvent(Event event) {
        //Get Message Path and originator User's ID from event
        String messagePath = (String) event.getProperty("path");
        String senderId = (String) event.getProperty("userId");
        MessagingEvent.MessagingActions action = (MessagingEvent.MessagingActions) event.getProperty("action");
        try{
            if(MessagingEvent.MessagingActions.MessageSent.equals(action)){
                resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

                //Read message
                Resource resource = resourceResolver.getResource(messagePath);
                Message msg = resource.adaptTo(Message.class);

                //Get list of recipient Ids from message
                //For Getting Started Tutorial, Id is same as email. If that is not the case in your site,
                //an additional step is needed to retrieve the email for the Id
                List<String> reclist = msg.getRecipientIdList();
                for(int i=0;i<reclist.size();i++){
                    //Send Email using Mailing Service
                    sendMail("admin@cqadmin.qqq",reclist.get(i),"New message on Getting Started Tutorial", "Hi\nYou have received a new message from  " +  senderId + ". To read it, sign in to Getting Started Tutorial.\n\n-Engage Admin");
                }
            }
        } catch(Exception ex){
            logger.error("Error getting message info : " + ex.getMessage());
        } finally {
            resourceResolver.close();
        }

    }
}

On this page