Messaging Essentials

本页文档了使用消息组件在网站上包含消息传递功能的详细信息。

客户端的必备工具

撰写消息

resourceType

social/messaging/components/hbs/composemessage

clientlibs

cq.social.hbs.messaging

模板 /libs/social/messaging/components/hbs/composemessage/composemessage.hbs
css /libs/social/messaging/components/hbs/composemessage/clientlibs/composemessage.css
属性 请参阅配置消息
管理员配置 配置消息传递

邮件列表 (针对收件箱、已发送和垃圾桶)

resourceType

social/messaging/components/hbs/messagebox

clientlibs

cq.social.hbs.messaging

模板 /libs/social/messaging/components/hbs/messagebox/messagebox.hbs
css /libs/social/messaging/components/hbs/messagebox/clientlibs/messagebox.css
属性 请参阅配置消息
管理员配置 配置消息传递

另请参阅客户端自定义

服务器端的必备工具

注意

对于以下MessageBuilder方法,字符串参数必须*不包含尾随斜杠“/”:

  • setInboxPath()
  • setSentItemsPath()

例如:

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

社区站点

使用向导创建的社区站点结构将在选中时包含消息功能。 请参阅社区站点控制台User Management设置。

示例代码:收到的消息通知

社交消息功能为操作抛出事件,例如sendmarking readmarking delete。 可以捕获这些事件并对事件中包含的数据采取操作。

以下示例是事件处理函数,它监听message sent事件,并使用Day CQ Mail Service向所有消息收件人发送电子邮件。

要试用服务器端示例脚本,您需要开发环境和构建OSGi捆绑包的能力。

  1. 以管理员身份登录 [CRXDE|Lite](http://localhost:4502/crx/de)

  2. /apps/engage/install中创建具有任意名称的bundle node,如

    • 符号名称:com.engage.media.social.messaging.MessagingNotification
    • 名称:入门教程消息通知
    • 描述:向用户发送电子邮件通知的示例服务在用户收到消息时
    • : com.engage.media.social.messaging.notification
  3. 导航至 /apps/engage/install/com.engage.media.social.messaging.MessagingNotification/src/main/java/com/engage/media/social/messaging/notification

    1. 删除自动创建的Activator.java
    2. 创建类MessageEventHandler.java
    3. 将下面的代码复制/粘贴到MessageEventHandler.java
  4. 单击​保存全部

  5. 导航到/apps/engage/install/com.engage.media.social.messaging.MessagingNotification/com.engage.media.social.messaging.MessagingNotification.bnd并添加在MessageEventHandler.java代码中编写的所有导入语句。

  6. 构建捆绑包

  7. 确保Day CQ Mail ServiceOSGi服务已配置

  8. 以一个演示用户身份登录并向另一个用户发送电子邮件

  9. 收件人应收到有关新邮件的电子邮件

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

Adobe Summit Banner

A virtual event April 27-28.

Expand your skills and get inspired.

Register for free
Adobe Summit Banner

A virtual event April 27-28.

Expand your skills and get inspired.

Register for free
Adobe Maker Awards Banner

Time to shine!

Apply now for the 2021 Adobe Experience Maker Awards.

Apply now
Adobe Maker Awards Banner

Time to shine!

Apply now for the 2021 Adobe Experience Maker Awards.

Apply now