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

后端 未结 2 1347
眼角桃花
眼角桃花 2021-02-04 17:48

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 wo

相关标签:
2条回答
  • 2021-02-04 18:25

    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.

    0 讨论(0)
  • 2021-02-04 18:37

    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.

    0 讨论(0)
提交回复
热议问题