Reputation:
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.
Upvotes: 5
Views: 1975
Reputation: 3603
For this you need to edit 2 classes of Smack 4.1
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) {
}
Upvotes: 3
Reputation: 3316
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.
Upvotes: 4