Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xmpp

smack

Hi is there any way to do android xmpp client which will be able to get message receive confirmation (XEP-0184) I read that there is XEP-0184 in smack but normal smack is not working with android(or I can't do it) there is always SASL authentication exception.

like image 933
Mathew1990 Avatar asked Jun 28 '13 17:06

Mathew1990


2 Answers

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.

like image 150
Flow Avatar answered Apr 29 '23 11:04

Flow


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.

like image 39
fr1550n Avatar answered Apr 29 '23 11:04

fr1550n