本頁記錄了使用消息傳遞元件在網站上包含消息傳遞功能的詳細資訊。
撰寫訊息
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 |
屬性 | 請參閱配置消息傳遞 |
管理員設定 | 配置消息 |
另請參閱客戶端定制
字串參數必須not包含下列MessageBuilder方法的尾隨斜線"/":
setInboxPath
()setSentItemsPath
()例如:
valid: mb.setInboxPath( "/mail/inbox" );
not valid: mb.setInboxPath( "/mail/inbox/" );
使用嚮導建立的社區站點結構在選定時包括消息傳遞功能。 請參閱社群網站主控台的User Management
設定。
「社交訊息」功能會拋出操作的事件,例如send
、marking read
、marking delete
。 可以捕獲這些事件並對事件中包含的資料採取操作。
以下範例是事件處理常式,其偵聽message sent
事件,並使用Day CQ Mail Service
傳送電子郵件給所有訊息收件者。
若要試用伺服器端範例指令碼,您需要開發環境和建立OSGi套件的能力:
以管理員身份登錄至 [CRXDE|Lite](https://localhost:4502/crx/de)
。
在/apps/engage/install
中以任意名稱建立bundle node
,例如:
com.engage.media.social.messaging.MessagingNotification
com.engage.media.social.messaging.notification
導覽至/apps/engage/install/com.engage.media.social.messaging.MessagingNotification/src/main/java/com/engage/media/social/messaging/notification
,然後:
Activator.java
類。MessageEventHandler.java
。MessageEventHandler.java
中。按一下保存全部。
導覽至/apps/engage/install/com.engage.media.social.messaging.MessagingNotification/com.engage.media.social.messaging.MessagingNotification.bnd
,並新增所有在MessageEventHandler.java
程式碼中寫入的匯入陳述式。
建立套件。
確保已配置Day CQ Mail Service
OSGi服務。
以示範使用者身分登入,並傳送電子郵件給其他使用者。
收件者會收到有關新訊息的電子郵件。
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();
}
}
}