Sushant Sushant - 3 months ago 11
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;

public String getElementName() {
return ELEMENT;

public String getNamespace() {

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

public static class Provider extends EmbeddedExtensionProvider {

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

return new TimestampExtension();

Registering provider

TimestampExtension.ELEMENT, TimestampExtension.NAMESPACE,
new TimestampExtension.Provider());

Sending message with extension

TimestampExtension timeExt = new TimestampExtension();

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>

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'>

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


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,;

    //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

    //Send message

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

    Message message = (Message) packet;

    //Get the extension from message
    DefaultPacketExtension extTimestamp = (DefaultPacketExtension) message

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

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