Sushant Sushant - 2 months ago 8
Android Question

How to add custom extension(time) to <message> tag?

I'm trying to add time extension to message with custom packet extension. It seems like time gets added while sending message. But at receiver end, it shows null.

This is my packet extension class and EmbeddedExtensionProvider

public class TimestampExtension implements PacketExtension {

public static final String ELEMENT = "timestamp";
public static final String NAMESPACE = "urn:xmpp:timestamp";

public String TIME = null;

public TimestampExtension() {

}

public void setTime() {
SimpleDateFormat time = new SimpleDateFormat("hh:mm a");
Date dateTime = new Date();
String strTime = time.format(dateTime);
this.TIME = strTime;
}

public String getTime() {
return TIME;
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public String getNamespace() {
return NAMESPACE;
}

@Override
public String toXML() {
return "<" + ELEMENT + " xmlns='" + NAMESPACE + "'><time>" + TIME
+ "</time></" + ELEMENT + ">";
}

public static class Provider extends EmbeddedExtensionProvider {

@Override
protected PacketExtension createReturnExtension(String arg0,
String arg1, Map<String, String> arg2,
List<? extends PacketExtension> arg3) {

return new TimestampExtension();
}
}
}


Registering provider

ProviderManager.getInstance().addExtensionProvider(
TimestampExtension.ELEMENT, TimestampExtension.NAMESPACE,
new TimestampExtension.Provider());


Sending message with extension

TimestampExtension timeExt = new TimestampExtension();
timeExt.setTime();
msg.addExtension(timeExt);
connection.sendPacket(msg);


Message xml output at sender:

<message id="m55K5-7" to="testfirzan@sushant" type="chat">
<body>hi</body><request xmlns='urn:xmpp:receipts'/>
<timestamp xmlns='urn:xmpp:timestamp'>
<time>01:04 PM</time>
</timestamp>




Message xml output at receiver:

<message id="m55K5-7" to="testfirzan@sushant" from="testsushant@sushant/Smack" type="chat">
<body>hi</body><request xmlns='urn:xmpp:receipts'/>
<timestamp xmlns='urn:xmpp:timestamp'>
<time>null</time>
</timestamp>




It's supposed to show sending time in time element. What i'm doing wrong ?

Answer

Sorry folks for late answer.

Since time was mandatory parameter for me, i end up using DefaultPacketExtension

//To sent message
private void sendMessage(String message, String recipient) {

    Message msg = new Message(recipient, Message.Type.chat);
    msg.setBody(message);

    //Getting current timestamp in string format
    String messageTimeStamp = String.valueOf(System.currentTimeMillis());

    //Creating default packet extension with name as 'timestamp' and urn as 'urn:xmpp:timestamp'
    DefaultPacketExtension extTimeStamp = new DefaultPacketExtension(
                                        "timestamp", "urn:xmpp:timestamp");

    //Setting value in extension
    extTimeStamp.setValue("timestamp", messageTimestamp);

    //Add extension to message tag
    msg.addExtension(extTimeStamp);

    //Send message
    xmppConnection.sendPacket(message);
}


//To receive and parse message with extension implement PacketListener
@Override
public void processPacket(Packet packet) {

    Message message = (Message) packet;

    //Get the extension from message
    DefaultPacketExtension extTimestamp = (DefaultPacketExtension) message
                    .getExtension("urn:xmpp:timestamp");

    //Get the value from extension
    long timestamp = Long.parseLong(extTimestamp.getValue("timestamp"));

    System.out.println("Message :" + message.getBody() + " Timestamp: "+timestamp);
}
Comments