如何将自定义扩展名(时间)添加到<message>标记?

时间:2015-04-29 07:42:48

标签: android time xmpp message

我尝试使用自定义数据包扩展功能为邮件添加时间扩展。似乎在发送消息时添加了时间。但在接收端,它显示为null。

这是我的数据包扩展类和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();
        }
    }
}

注册提供商

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

发送带扩展名的邮件

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

发件人的消息xml输出:

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

接收方的消息xml输出:

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

它应该显示发送时间元素。我做错了什么?

3 个答案:

答案 0 :(得分:5)

Sushant的回答是正确的,但最新版本的org.igniterealtime.smack:smack-android:4.2.0-beta2他们已将DefaultPacketExtension更改为StandardExtensionElement,以便在邮件中添加自定义元素。

添加扩展程序的代码

//Creating Standard packet extension with name as 'timestamp' and urn as 'urn:xmpp:timestamp'
 StandardExtensionElement extTimeStamp = StandardExtensionElement.builder(
        "timestamp", "urn:xmpp:timestamp")
        .addAttribute("timestamp", String.valueOf(System.currentTimeMillis()))  //Setting value in extension
        .build();


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

希望这将有助于未来的开发者。

答案 1 :(得分:2)

对不起,请大家回答。

由于时间是我的强制性参数,我最终使用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);
}

答案 2 :(得分:0)

问题出在您的提供商

        return new TimestampExtension();

在解析消息时需要从XmlPullParser获取time元素并将其设置为扩展实例。