Send composing message using SMACK

Hi All!

Tell me please how I can send message of this type: <composing xmlns='http://jabber.org/protocol/chatstates’/> ???

Example code would be very good

Thank for answer!!!

I think what you are looking for is

ChatStateManager.getInstance(connection).setCurrentState(ChatState.composing, myChat);

Thanks for answer.

Problem was solved.

Example:

  1. Create class MessagePacketExtension implements PacketExtension

public class MessagePacketExtension implements PacketExtension {

@Override

public String getElementName() {

return “composing”;

}

@Override

public String getNamespace() {

    return "[http://jabber.org/protocol/chatstates](http://jabber.org/protocol/chatstates)";

}

@Override

public String toXML() {

    return "<composing xmlns='[http://jabber.org/protocol/chatstates'/](http://jabber.org/protocol/chatstates'/)>";

}

}

  1. Use it.

Message _packet = new Message();

_packet.addExtension(new MessagePacketExtension(getTypeNotification(req)));

_packet.setTo(_to);

_sessionData.getXmppConnection().sendPacket(_packet);

All works!!!

You should probably try the existing solution instead of rolling your own.

Just add ChatStateManager after ChatManager intalization

chatManager = ChatManager.getInstanceFor(getXmpptcpConnection());

ChatStateManager.getInstance(getXmpptcpConnection());

then you need to add ChatStateListener during createChat(to,chatMesageListener)

chatManager.createChat(message.getTo(), chatMessageListener).sendMessage(message);

private ChatStateListener chatMessageListener = new ChatStateListener() {

@Override

public void stateChanged(Chat chat, ChatState state) {

//State Change composing,active,paused,gone,etc

Log.d(TAG, "ChatStateListener:::stateChanged -> " + chat.toString() + " \n -> " + state.toString());

}

@Override

public void processMessage(Chat chat, Message message) {

//Incoming Message

Log.d(TAG, "ChatStateListener:::processMessage -> " + chat.toString() + " \n -> " + message.toString());

}

};

Thanks for your answer.

I am getting exception at the below line when processing packets

Message msg = (Message) packet;

org.jivesoftware.smack.packet.Presence cannot be cast to org.jivesoftware.smack.packet.Message

Not every Packet is a Message. You appear to be trying to cast a Presence into a Message, which is impossible.

1 Like

Yeah you are right. Thank you so much

Hi guys,

@Paul_Schaub do you have an example of this with 4.2.4 or 4.3.0-rc1 but for sending? (incoming one is easy)
I mean, I’m trying something like @Dastanlqbal said but it seems it has changed a little bit.

Please take a look at the chatstates package in smacks experimental (or was it smacks extensions?), the code should be pretty self explanatory.

Thx Paul,

Looking the source code I found how to do it.

ChatStateManager.getInstance(connection).setCurrentState(ChatState.composing, chat)

1 Like