4

I am trying to send a chat using Smack. Here is the code

  Chat chat = connection.getChatManager().createChat("2@coolcast.com", this);
  Message _msg = new Message();
  _msg.setBody("Hello this is a test message");
  chat.sendMessage(_msg);

This message shows up at the recipient's end with type = "chat". How can I change this message type to a custom string e.g. "my_custom_string"?

I tried this but it is easy to see why the following won't work

  Message.Type _type = Message.Type.fromString("my_custom_string");
  _msg.setType(_type);
S B
  • 8,134
  • 10
  • 54
  • 108
  • 3
    If you find yourself ever in a situation where you need to set a custom message type string, then you should rethink your design! That's not the way how XMPP should be modified. You could, for example, simply add a custom element under the message element (a packetextension in smack). – Flow Jan 28 '14 at 09:36
  • How did you solve the issue? or just extends the Message to add a custom element? – Stony Mar 18 '14 at 03:32
  • I solved it by using the subject field for specifying custom types. A more generic solution would be to implement Flow's suggestion – S B Mar 18 '14 at 04:31

1 Answers1

0

Message is final class in smack, also it follows the xmpp protocol for message Stanza, so you cannot modify the Type field in Message. But we cannot end giving up, here you can try a small trick by adding your custom extension in Message Stanza you just need to create a ExtensionElement using provider architecture of smack and then add it to Message Packet by calling addExtension() method on Message Object.

it will be like this :

 <message from="demo@mydomain.com" to="demo2@mydomain.com" type="chat">
    <body>Hi this is demo 1</body>
    <my_custom_element xmlns="some_name_space" attributr="some_attribute">Some custom message</my_custom_element>
</message> 

you can multiple Extenstion to any Packet/Stanza in smack.

to learn more about Smack Provider Architecture follow this link

Tushar Purohit
  • 524
  • 7
  • 25