如何将自定义扩展名(时间)添加到 <message> 标签?
How to add custom extension(time) to <message> tag?
我正在尝试使用自定义数据包扩展为消息添加时间扩展。发送消息时似乎增加了时间。但是在接收端,它显示为空。
这是我的数据包扩展 class 和 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>
应该在时间元素中显示发送时间。我做错了什么?
问题出在您的提供商
return new TimestampExtension();
您需要在解析消息时从 XmlPullParser 中获取时间元素并将其设置为您的扩展实例。
抱歉大家回答晚了。
因为时间对我来说是强制性参数,所以我最终使用 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);
}
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);
希望这对未来的开发者有所帮助。
我正在尝试使用自定义数据包扩展为消息添加时间扩展。发送消息时似乎增加了时间。但是在接收端,它显示为空。
这是我的数据包扩展 class 和 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>
应该在时间元素中显示发送时间。我做错了什么?
问题出在您的提供商
return new TimestampExtension();
您需要在解析消息时从 XmlPullParser 中获取时间元素并将其设置为您的扩展实例。
抱歉大家回答晚了。
因为时间对我来说是强制性参数,所以我最终使用 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);
}
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);
希望这对未来的开发者有所帮助。