Nozioni di base sulla messaggistica

Questa pagina illustra i dettagli dell’utilizzo del componente Messaggistica per includere una funzione di messaggistica su un sito web.

Funzionalità di base per lato client

Componi messaggio

resourceType

social/messaging/components/hbs/compositemessage

clientlibs

cq.social.hbs.messaging

templates /libs/social/messaging/components/hbs/composemessage/composemessage.hbs
css /libs/social/messaging/components/hbs/composemessage/clientlibs/composemessage.css
proprietà Vedi Configurare la messaggistica
configurazione amministratore Configurare la messaggistica

Elenco messaggi

(per Posta in arrivo, Inviata e Cestino)

resourceType

social/messaging/components/hbs/messagebox

clientlibs

cq.social.hbs.messaging

templates /libs/social/messaging/components/hbs/messagebox/messagebox.hbs
css /libs/social/messaging/components/hbs/messagebox/clientlibs/messagebox.css
proprietà Vedi Configurare la messaggistica
configurazione amministratore Configurare la messaggistica

Vedi anche Personalizzazioni lato client

Funzioni di base per lato server

ATTENZIONE

Il parametro String deve not contiene una barra finale "/" per i seguenti metodi di MessageBuilder:

  • setInboxPath()
  • setSentItemsPath()

Esempio:

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

Sito community

Una struttura del sito community, creata utilizzando la procedura guidata, include la funzione di messaggistica selezionata. Vedi User Management impostazioni di Console Sites della community.

Codice di esempio: Messaggio ricevuto notifica

La funzione Messaggistica social genera eventi per le operazioni, ad esempio send, marking read, marking delete. Questi eventi possono essere rilevati e le azioni intraprese sui dati contenuti nell’evento.

L'esempio seguente è di un gestore eventi che ascolta il message sent e invia un’e-mail a tutti i destinatari del messaggio utilizzando Day CQ Mail Service.

Per provare lo script di esempio lato server, è necessario un ambiente di sviluppo e la capacità di creare un bundle OSGi:

  1. Accedi come amministratore a [CRXDE|Lite](https://localhost:4502/crx/de).

  2. Crea un bundle nodein /apps/engage/install con nomi arbitrari, ad esempio:

    • Nome simbolico: com.engage.media.social.messaging.MessagingNotification
    • Nome: Notifica messaggio tutorial introduttiva
    • Descrizione: Un servizio di esempio per l’invio di una notifica e-mail agli utenti che ricevono un messaggio
    • Pacchetto: com.engage.media.social.messaging.notification
  3. Passa a /apps/engage/install/com.engage.media.social.messaging.MessagingNotification/src/main/java/com/engage/media/social/messaging/notificatione quindi:

    1. Elimina Activator.java classe creata automaticamente.
    2. Crea classe MessageEventHandler.java.
    3. Copia e incolla il codice sottostante in MessageEventHandler.java.
  4. Fai clic su Salva tutto.

  5. Passa a /apps/engage/install/com.engage.media.social.messaging.MessagingNotification/com.engage.media.social.messaging.MessagingNotification.bnde aggiungi tutte le istruzioni di importazione come scritte nella MessageEventHandler.java codice.

  6. Crea il bundle.

  7. Assicurati Day CQ Mail ServiceIl servizio OSGi è configurato.

  8. Accedi come utente dimostrativo e invia un’e-mail a un altro utente.

  9. Il destinatario riceve un’e-mail relativa a un nuovo messaggio.

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();
        }

    }
}

In questa pagina