How to add custom fields in <message> elements of XMPP stanza/packet?
Asked Answered
Y

2

5

I want to send

<message id="qm5Dx-8"
 to="abc"
 type="chat" 
 from="abc"
 msgType="2"
 msgTimeStamp="1413971599039"
 fileSize="18 MB" 
 fileHeight="300"
 fileWidth="300"
 thumbnail="abc"
 mediaURL=""
 serverMediaURL="xyz"
 isFromMe="1"
 status="1"><body>Image</body><request xmlns='urn:xmpp:receipts'/></message>

The way i am constructing the custom message is :

public class MyCustomMessage  extends Message{

    public MyCustomMessage(){
        super();
    }

    public MyCustomMessage(String to, Type type){
        super(to, type);
    }
    private String msgType ;
    private String msgTimeStamp ;
    private String isFromMe ;
    private String status ;
    private String mediaURL ;
    private String serverMediaURL ;
    private String fileSize ;
    private String fileHeight ;
    private String fileWidth ;
    private String thumbnail ;

    @Override
    public String toXML() {
        String XMLMessage = super.toXML();
        String XMLMessage1 = XMLMessage.substring(0, XMLMessage.indexOf(">"));
        String XMLMessage2 = XMLMessage.substring(XMLMessage.indexOf(">"));

        if (this.msgType != null) {
            XMLMessage1 += " msgType=\"" + this.msgType + "\"";
        }
        if (this.msgTimeStamp != null) {
            XMLMessage1 += " msgTimeStamp=\"" + this.msgTimeStamp + "\"";
        }
        if (this.fileSize != null) {
            XMLMessage1 += " fileSize=\"" + this.fileSize + "\"";
        }
        if (this.fileHeight != null) {
            XMLMessage1 += " fileHeight=\"" + this.fileHeight + "\"";
        }
        if (this.fileWidth != null) {
            XMLMessage1 += " fileWidth=\"" + this.fileWidth + "\"";
        }
        if (this.thumbnail != null) {
            XMLMessage1 += " thumbnail=\"" + this.thumbnail + "\"";
        }
        if (this.mediaURL != null) {
            XMLMessage1 += " mediaURL=\"" + this.mediaURL + "\"";
        }
        if (this.serverMediaURL != null) {
            XMLMessage1 += " serverMediaURL=\"" + this.serverMediaURL + "\"";
        }
        if (this.isFromMe != null) {
            XMLMessage1 += " isFromMe=\"" + this.isFromMe + "\"";
        }
        if (this.status != null) {
            XMLMessage1 += " status=\"" + this.status + "\"";
        }

        return XMLMessage1 + XMLMessage2;
    }

// Setters Getters of all these fields..

}

Then after adding required fields in SmackableImplement class, i m calling mXMPPConnection.sendPacket(customMessage);

but m not receiving any packet. my connections is being closed everytime after calling this method. I have gone through many tutorials but couldnt find any solution...tell me where m mistaken.

Yorick answered 22/10, 2014 at 12:44 Comment(0)
M
6
  1. Smack surely has a better way to work with XML than this approach with modifying the string representation. This will break badly when anything contains a " or any of the other characters that need to be escaped as an attribute.

  2. You have to add custom payloads to messages as a separate XML element within the message, not as attributes on the message. Your XML should look like:

     <message id="qm5Dx-8" to="abc" type="chat" from="abc">
        <body>Image</body>
        <request xmlns='urn:xmpp:receipts'/>
        <data xmlns='http://bstkaal/custom/data'
          msgType="2"
          msgTimeStamp="1413971599039"
          fileSize="18 MB" 
          fileHeight="300"
          fileWidth="300"
          thumbnail="abc"
          mediaURL=""
          serverMediaURL="xyz"
          isFromMe="1"
          status="1" />
    </message>
    
Minefield answered 22/10, 2014 at 14:5 Comment(0)
I
2

xnyhps answer is correct. I've just want to add a few things. He already said that, but I can't stress the fact enough, because I see it again and again:

Never add custom values to specified stream element attributes (e.g. a new value for the type attribute of messages), and never add new attributes to top level elements (like you did with msgType, msgTimeStamp and so on).

This has the potential to break things! Don't do it. See also "XEP-0134: XMPP Design Guidelines § 2.1 XMPP is Sacred". That's why it's not possible in Smack. Instead, use a custom extension element, like xnyhps showed in his example (the data element). See also "RFC 6120 § 8.4 Extended Content" Those are called PacketExtension's in Smack.

Indenture answered 22/10, 2014 at 15:10 Comment(1)
as u said, i have tried PacketExtension, but not getting my results. can u plz look at this post #26577823Yorick

© 2022 - 2024 — McMap. All rights reserved.