To do this, you need to edit the 2 classes of Smack 4.1
- Stanza class ( 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 Constructor Stanza
protected Stanza(Stanza p) {
Change the addCommonAttributes method
protected void addCommonAttributes(XmlStringBuilder xml) { //add this line if(getNick()!= null) xml.optAttribute("nick", getNick()); }
2. Class PacketParserUtils
Change parseMessage method
public static Message parseMessage(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException {
Now you can simply set the nickname and send the 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) { }
source share