如何在Smack中使用XMPP XEP-0184“消息发送收据”?

时间:2013-06-28 17:33:33

标签: xmpp smack

嗨有没有办法做android xmpp客户端,它将能够获得消息接收确认(XEP-0184)我读到在smack中有XEP-0184但是正常的smack不能用于android(或者我可以' t do)总是有SASL身份验证例外。

2 个答案:

答案 0 :(得分:15)

Smack通过SMACK-331获得了对XEP-0184的支持。你不能使用Smack< 4.1直接在Android下,你需要Smack 4.1(或更高版本)。

您可以在DeliveryReceiptManager的javadoc中阅读有关Smack的XEP-0184 API的更多信息。

答案 1 :(得分:11)

是的,这适用于普通的Smack。

Gradle依赖

compile "org.igniterealtime.smack:smack-android:4.1.0"
compile "org.igniterealtime.smack:smack-tcp:4.1.0"
compile "org.igniterealtime.smack:smack-extensions:4.1.0" // <-- XEP-0184 classes

准备XMPPTCPConnection,即在连接()之前连接处理程序以获取送达回执

DeliveryReceiptManager.getInstanceFor(mConnection).addReceiptReceivedListener(new ReceiptReceivedListener() {
        @Override
        public void onReceiptReceived(String fromJid, String toJid, String deliveryReceiptId, Stanza stanza) {
            Log.d(TAG, "onReceiptReceived: from: " + fromJid + " to: " + toJid + " deliveryReceiptId: " + deliveryReceiptId + " stanza: " + stanza);
        }
    }); 

发送邮件时,请确保包含MessageReceiptRequest

Chat chat;
if (StringUtils.isNullOrEmpty(threadId)) {
    chat = getChatManager().createChat(to);
    Log.d(TAG, "sendMessage: no thread id so created Chat with id: " + chat.getThreadID());
} else {
    chat = getChatManager().getThreadChat(threadId);
    Log.d(TAG, "sendMessage: thread id was used to continue this chat");
}
Message message = new Message(to);
message.addBody("EN", messageText);
String deliveryReceiptId = DeliveryReceiptRequest.addTo(message);
chat.sendMessage(message);
Log.d(TAG, "sendMessage: deliveryReceiptId for this message is: " + deliveryReceiptId);

全部完成

现在,您可以判断对方是否收到了已发送的消息,因为上面的Chat.sendMessage(消息)代码中获取的deliveryReceiptId将由之前设置的onReceiptReceived回调记录。