How to add custom fields to <message> elements of stanza / XMPP package elements?

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> 

A way to create a custom message:

 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 the required fields to the SmackableImplement class, I call mXMPPConnection.sendPacket (customMessage);

but m does not receive any packet. my connections are closed every time I call this method. I went through a lot of tutorials but couldn't find any solution ... tell me where m is wrong.

+3
source share
2 answers
  • Smack probably has a better way to work with XML than this approach with changing the string representation. This can go bad when something contains " or any other character that needs to be escaped as an attribute.

  • You should add custom useful messages to messages as a separate XML element in the message, and not as message attributes. Your XML should look like this:

      <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> 
+4
source

xnyhps the answer is correct. I just want to add a few things. He has already said this, but I cannot stress this fact enough, because I see him again and again:

Never add special values ​​to the specified attributes of a stream element (for example, a new value for a message type attribute) and never add new attributes to top-level elements (for example, with msgType , msgTimeStamp , and so on).

It has the potential to break things! Do not do this. See also "XEP-0134: XMPP Design Guide Β§ 2.1 XMPP is Sacred" . This is why this is not possible in Smack. Instead, use your own extension element, for example, xnyhps, shown in its example ( data element). See Also "RFC 6120 Β§ 8.4 Extended Content" They are called PacketExtension in Smack.

+2
source

All Articles