How can I make use of XMPP XEP-0184 “Message Delivery Receipts” with Smack?

帅比萌擦擦* 提交于 2019-12-03 01:28:01

Smack received support for XEP-0184 with SMACK-331. You can't use Smack < 4.1 directly under Android, you need Smack 4.1 (or higher).

You can read more about Smack's XEP-0184 API in the javadoc of DeliveryReceiptManager.

Yes this works with normal Smack.

Gradle Dependencies

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

Prepare the XMPPTCPConnection i.e. before you connect() wire up a handler for when you get a delivery receipt

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);
        }
    }); 

When sending a message, ensure you include a 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);

All done

Now, you can tell when a sent message has been received by the other side because the deliveryReceiptId obtained in the Chat.sendMessage(Message) code above will be logged by the onReceiptReceived callback set up earlier.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!