メッセージングの基本事項 messaging-essentials

このページでは、メッセージングコンポーネントを使用してweb サイトにメッセージ機能を含める方法を詳しく説明します。

クライアントサイドの基本 essentials-for-client-side

メッセージを作成

resourceType
ソーシャル/メッセージ/コンポーネント/hbs/コンポーネントメッセージ
clientllibs
cq.social.hbs.messaging
テンプレート
/libs/social/messaging/components/hbs/composemessage/composemessage.hbs
css
/libs/social/messaging/components/hbs/composemessage/clientlibs/composemessage.css
properties
メッセージの設定を参照してください
管理者設定
メッセージングの設定

メッセージリスト

(受信トレイ、送信済み、およびゴミ箱の場合)

resourceType
ソーシャル/メッセージ/コンポーネント/hbs/messagebox
clientllibs
cq.social.hbs.messaging
テンプレート
/libs/social/messaging/components/hbs/messagebox/messagebox.hbs
css
/libs/social/messaging/components/hbs/messagebox/clientlibs/messagebox.css
properties
メッセージの設定を参照してください
管理者設定
メッセージングの設定

​ クライアントサイドのカスタマイズ ​も参照してください

サーバーサイドの基本 essentials-for-server-side

CAUTION
String パラメーターには、次のMessageBuilder メソッドの末尾のスラッシュ「/」を​ not ​に含める必要があります。
  • setInboxPath()
  • setSentItemsPath()
例:
code language-none
valid: mb.setInboxPath( "/mail/inbox" );
    not valid: mb.setInboxPath( "/mail/inbox/" );

コミュニティサイト community-site

ウィザードを使用して作成されたコミュニティサイト構造には、選択時にメッセージ機能が含まれます。 ​ コミュニティサイトコンソール ​User Management設定を参照してください。

サンプルコード:メッセージ受信通知 sample-code-message-received-notification

ソーシャル メッセージ機能は、操作のイベント (例:sendmarking readmarking delete)をスローします。 これらのイベントは、イベントに含まれるデータに対してキャッチおよびアクションを実行できます。

次の例は、message sent イベントをリッスンし、Day CQ Mail Serviceを使用してすべてのメッセージ受信者に電子メールを送信するイベントハンドラーです。

サーバーサイドのサンプルスクリプトを試すには、開発環境とOSGi バンドルを構築する機能が必要です。

  1. 管理者として [CRXDE|Lite](https://localhost:4502/crx/de)にログインします。

  2. 次のような任意の名前を持つbundle node/apps/engage/installに作成します。

    • シンボル名: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 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();
        }

    }
}
recommendation-more-help
experience-manager-65-help-communities