How can I add custom attribute (nick) in my XMPP message tag, in Smack 4.1
Asked Answered
R

2

5

I want to add custom attribute (nick) in my XMPP chat message, like the following example

<message from='*' to='*' id='123' nick='KASHIF' type='chat'><body>hello</body></message>

I know, it is not recommended by XMPP but it is my requirement as this attribute(nick) is already implemented in the iOS version of the app i am working on.

Roussel answered 11/5, 2015 at 6:42 Comment(0)
L
3

For this you need to edit 2 classes of Smack 4.1

  • Stanza class in (org.jivesoftware.smack.packet)
  • PacketParserUtils class in (org.jivesoftware.smack.util)

1. Stanza class

Define your custom attribute (nick)

 private String nick = null;

Define Getter and Setters

public String getNick() {
    return this.nick;
}

public void setNick(String paramString) {
   this.nick = paramString;
}

Edit Stanza Constructor

protected Stanza(Stanza p) {

   //add this line
   nick = p.getNick();

}

Edit addCommonAttributes method

protected void addCommonAttributes(XmlStringBuilder xml) {

    //add this line
    if(getNick()!= null)
         xml.optAttribute("nick", getNick());
}

2. PacketParserUtils class

Edit parseMessage method

 public static Message parseMessage(XmlPullParser parser)
                throws XmlPullParserException, IOException, SmackException {

   //add this line
   message.setNick(parser.getAttributeValue("", "nick"));

}

Now you can simply set nick and send message as follows

Message message = new Message();
message.setType(Message.Type.chat);
message.setStanzaId("123");
message.setTo(number);

message.setNick("SHAYAN");    

try {
 connection.sendStanza(message);
} catch (NotConnectedException e) {
}
Lucullus answered 11/5, 2015 at 7:28 Comment(9)
Hi shanrais!, I cloned Smack from github and I edited two classes. But, you didn't say how to deploy smack and create jars again to Android with right ?Permanent
Instead of creating Jars, What I have done is included the source code of that jar which I have edited in my project.Lucullus
can you say which folders need copy? Have many folders in project github.com/igniterealtime/Smack on github, I don't know which folders need copy to my project. Following jars declared in my app gradle: compile 'org.igniterealtime.smack:smack-android-extensions:4.1.1' compile 'org.igniterealtime.smack:smack-experimental:4.1.1' compile 'org.igniterealtime.smack:smack-tcp:4.1.1'Permanent
Do not copy smack-core-4.1.0.jar in your lib folder. Just copy the code of smack-core-4.1.0-sources in your project.Lucullus
can you example project? I added just source code smack-core-4.1.0-sources but appear another error: not found import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.parts.Resourcepart;Permanent
try copying both code(smack-core-4.1.0-sources) + library(smack-core-4.1.0.jar) in your lib folderLucullus
Hey @shanrais I need to do the same thing in my app. Can you please tell me exact steps of doing it? As there are so many projects so I am really confused.Costmary
Is there any way to add custom attributes without modifying smack library ?Bluetongue
you can use this approach : https://mcmap.net/q/1986416/-how-to-add-custom-fields-in-lt-message-gt-elements-of-xmpp-stanza-packet or use extension as mentioned by the xnyhps below https://mcmap.net/q/2008176/-how-can-i-add-custom-attribute-nick-in-my-xmpp-message-tag-in-smack-4-1Lucullus
B
4

Don't do that, it's not recommended for a reason. It's very likely some servers will strip the attribute or even completely refuse to handle the packet. Instead, the recommended way is to add a custom element.

In fact, such an extension already exists, XEP-0172:

<message from='*' to='*' id='123' type='chat'>
    <nick xmlns='http://jabber.org/protocol/nick'>KASHIF</nick>
    <body>hello</body>
</message>

This might already work with other clients or libraries, so it's a much better solution.

Borman answered 11/5, 2015 at 7:25 Comment(1)
in my case, this will crash the iOS version of the appRoussel
L
3

For this you need to edit 2 classes of Smack 4.1

  • Stanza class in (org.jivesoftware.smack.packet)
  • PacketParserUtils class in (org.jivesoftware.smack.util)

1. Stanza class

Define your custom attribute (nick)

 private String nick = null;

Define Getter and Setters

public String getNick() {
    return this.nick;
}

public void setNick(String paramString) {
   this.nick = paramString;
}

Edit Stanza Constructor

protected Stanza(Stanza p) {

   //add this line
   nick = p.getNick();

}

Edit addCommonAttributes method

protected void addCommonAttributes(XmlStringBuilder xml) {

    //add this line
    if(getNick()!= null)
         xml.optAttribute("nick", getNick());
}

2. PacketParserUtils class

Edit parseMessage method

 public static Message parseMessage(XmlPullParser parser)
                throws XmlPullParserException, IOException, SmackException {

   //add this line
   message.setNick(parser.getAttributeValue("", "nick"));

}

Now you can simply set nick and send message as follows

Message message = new Message();
message.setType(Message.Type.chat);
message.setStanzaId("123");
message.setTo(number);

message.setNick("SHAYAN");    

try {
 connection.sendStanza(message);
} catch (NotConnectedException e) {
}
Lucullus answered 11/5, 2015 at 7:28 Comment(9)
Hi shanrais!, I cloned Smack from github and I edited two classes. But, you didn't say how to deploy smack and create jars again to Android with right ?Permanent
Instead of creating Jars, What I have done is included the source code of that jar which I have edited in my project.Lucullus
can you say which folders need copy? Have many folders in project github.com/igniterealtime/Smack on github, I don't know which folders need copy to my project. Following jars declared in my app gradle: compile 'org.igniterealtime.smack:smack-android-extensions:4.1.1' compile 'org.igniterealtime.smack:smack-experimental:4.1.1' compile 'org.igniterealtime.smack:smack-tcp:4.1.1'Permanent
Do not copy smack-core-4.1.0.jar in your lib folder. Just copy the code of smack-core-4.1.0-sources in your project.Lucullus
can you example project? I added just source code smack-core-4.1.0-sources but appear another error: not found import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.parts.Resourcepart;Permanent
try copying both code(smack-core-4.1.0-sources) + library(smack-core-4.1.0.jar) in your lib folderLucullus
Hey @shanrais I need to do the same thing in my app. Can you please tell me exact steps of doing it? As there are so many projects so I am really confused.Costmary
Is there any way to add custom attributes without modifying smack library ?Bluetongue
you can use this approach : https://mcmap.net/q/1986416/-how-to-add-custom-fields-in-lt-message-gt-elements-of-xmpp-stanza-packet or use extension as mentioned by the xnyhps below https://mcmap.net/q/2008176/-how-can-i-add-custom-attribute-nick-in-my-xmpp-message-tag-in-smack-4-1Lucullus

© 2022 - 2024 — McMap. All rights reserved.