How can I make use of XMPP XEP-0184 “Message Deliv

2019-03-14 04:41发布

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.

标签: xmpp smack
2条回答
成全新的幸福
2楼-- · 2019-03-14 05:20

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.

查看更多
乱世女痞
3楼-- · 2019-03-14 05:28

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.

查看更多
登录 后发表回答