我正在使用smack和openfire在android中创建聊天应用程序。 对于消息状态我在其他情况下传送和显示消息没有问题 客户(仔细检查)。 我将向发件人发送一条简单的json消息,如bellow: { “交付”:timestapmp} 并解析它并仔细检查低于之前发送的时间戳的消息。 问题是关于发送状态(一次检查)。 当我发送消息时,服务器没有响应消息发送的任何内容。 是否有可能在smack中发送带有来自服务器回调的消息。 如果可能的话,是否可以在回调响应中发送时间服务器。 谢谢。
答案 0 :(得分:7)
private void acknowledgementFromServer(final Message message) throws StreamManagementException.StreamManagementNotEnabledException {
if (connection != null && connection.isSmEnabled()) {
connection.addStanzaIdAcknowledgedListener(message.getStanzaId(), new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
MessageAsyncTask task = new MessageAsyncTask(packet.getStanzaId(), MSG_STATUS_SENT);
task.execute();
}
});
}
嘿,你可以这样做..每次通过在上面的方法中将该消息作为参数传递来发送消息时调用方法
注意:应启用流管理以使其正常工作,可以如下所示:
DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());
答案 1 :(得分:2)
根据我的知识,我已经达到了这个Inteface:ReceiptReceivedListener
,它位于smack 4.2
下面是我如何实现这个:
private ReceiptReceivedListener receiptReceivedListener;
/**
* get DeliveryReceiptManager
*
* @return
*/
private DeliveryReceiptManager getDeliveryReceiptManager() {
if (deliveryReceiptManager == null && getConnection() != null) {
deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(getConnection());
}
return deliveryReceiptManager;
}
添加监听器
getDeliveryReceiptManager().addReceiptReceivedListener(receiptReceivedListener);
收到回电
receiptReceivedListener = new ReceiptReceivedListener() {
@Override
public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
//TODO : on recieved status of message delivery
}
};
这对您有帮助
以下是Smack 4.2界面的详细信息:
/**
* Callback invoked when a new receipt got received.
* <p>
* {@code receiptId} correspondents to the message ID, which can be obtained with
* {@link org.jivesoftware.smack.packet.Stanza#getStanzaId()}.
* </p>
*
* @param fromJid the jid that send this receipt
* @param toJid the jid which received this receipt
* @param receiptId the message ID of the stanza(/packet) which has been received and this receipt is for
* @param receipt the receipt
*/
void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt);