Vinod Patel
Vinod Patel

Reputation: 430

How to set custom XMPP Message.Type string in Smack?

MultiUserChatManager mucManager = MultiUserChatManager.getInstanceFor(conn);
MultiUserChat muc = mucManager.getMultiUserChat("[email protected]");

newMsg = muc.createMessage();
newMsg.setBody(strMsg);
newMsg.setType(Message.Type.groupchat);

My requirement is to pass custom message type in

newMsg.setType(Message.Type.groupchat);

For example instead of passing "Message.Type.groupchat" I want to pass "Message.Type.image" or some string "image" which is not present in type by default.

Upvotes: 1

Views: 1978

Answers (2)

Flow
Flow

Reputation: 24043

Setting a different message type then the defined ones in the XMPP RFC is not allowed as it would produce invalid message stanzas. Use a custom extension element added to the message stanza instead.

Instead of

<message type='my-custom-message-type-news' …>
  <body>
    This is a message with a custom message type 'news'.
    Do not do this!
  </body>
</message>

do

<message …>
  <body>
    This is a message with additional metadata found in a custom
    extension element. In this example the message is tagged as 'news'.
    That is how you extend XMPP and tag messages.
  </body>
  <my-custom-extension-element xmlns='https://my.company.com' type='news'>
    <more-metadata-if-you-want date='2018-04-03'/>
  </my-custom-extension-element>
</message>

Upvotes: 3

shanraisshan
shanraisshan

Reputation: 3603

You can add custom type by editing Message class in (org.jivesoftware.smack.packet)

in Enum defined your custom type

 public enum Type {

    /* CUSTOM TYPE */
    image

    /**
     * (Default) a normal text message used in email like interface.
     */
    normal,

    /**
     * Typically short text message used in line-by-line chat interfaces.
     */
    chat,

    /**
     * Chat message sent to a groupchat server for group chats.
     */
    groupchat,

    /**
     * Text message to be displayed in scrolling marquee displays.
     */
    headline,

    /**
     * indicates a messaging error.
     */
    error;

    /**
     * Converts a String into the corresponding types. Valid String values that can be converted
     * to types are: "normal", "chat", "groupchat", "headline" and "error".
     * 
     * @param string the String value to covert.
     * @return the corresponding Type.
     * @throws IllegalArgumentException when not able to parse the string parameter
     * @throws NullPointerException if the string is null
     */
    public static Type fromString(String string) {
        return Type.valueOf(string.toLowerCase(Locale.US));
    }

}

For Sending Messages, You can simply set your custom type (image) as follows :

Message message = new Message();
message.setType(Message.Type.image);
message.setStanzaId("123");
message.setTo(number);
try {
   connection.sendStanza(message);
} catch (NotConnectedException e) {
}

and For Receiving Message of custom type use following code

StanzaTypeFilter message_filter = new StanzaTypeFilter(Message.class);
connection.addSyncStanzaListener(new StanzaListener() {
    @Override
    public void processPacket(Stanza packet) throws NotConnectedException {
       Message message = (Message)packet;
       if(message.getType() == Message.Type.image) {
          //your code
       }
    }    
}, message_filter);

________________________________________________________________

EDIT : YOU NEED TO ADD THE SOURCE CODE FILES INTO YOUR PROJECT (DON'T NEED TO RECOMPILE THE JAR FILE)

________________________________________________________________

I am using Smack 4.1.2 in my project. You can download it from the link below https://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_4_1_2.zip

Unzip it, In libs folder each modules have 3 jar file for example

  1. smack-android-4.1.2.jar
  2. smack-android-4.1.2-javadoc.jar
  3. smack-android-4.1.2-sources.jar

All you need is simple jar files, Add the required jar files in your project lib folder. for example, I am using following jars, below is my app.gradle

compile files('libs/smack-core-4.1.2-sources.jar')
compile files('libs/smack-android-4.1.2.jar')
compile files('libs/smack-extensions-4.1.2.jar')
compile files('libs/smack-im-4.1.2.jar')
compile files('libs/smack-sasl-provided-4.1.2.jar')
compile files('libs/smack-tcp-4.1.2.jar')

Now, for customizing Smack (that is adding custom type), you need to add the code into your project

Unzip smack-core-4.1.2-sources.jar and add the java code files into your project and now you can edit the Message.java

Upvotes: 0

Related Questions