How to read custom attributes of xmpp message tag

2019-03-01 00:07发布

问题:

The xml that i need to receive is :

<message id="qm5Dx8" to="adsfxcv" type="chat"from="adsf"
 msgType="2"
 thumbnail="randomThumbnail"
 serverMediaURL="random"
 isFromMe="1"
 status="1"><body>Image</body><request xmlns='urn:xmpp:receipts'/></message>

Message is being sent by MyCustomMessage extends Message class.

In my message listener, Where i need to get the packet is :

            public void processPacket(Packet packet) {
            String recivedPacket = packet.toXML();
            try {
                if (packet instanceof MyCustomMessage) {
                    MyCustomMessage msg = (MyCustomMessage) packet;
                    ....

But I am receiving only id,to,type and from in message tag. And the instance of packet is also of Message and it says , cannot cast packet to message. plz guide me how can i receive my desired packet.

回答1:

You can't. Also you should never extend the Message or Presence class.

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.

See also this answer and question.