get delivery status of recent messages in smack

2019-03-02 06:18发布

问题:

I'm newby in smack 4.2.4 and xmpp. I've sent bunch of messages but the recipent is not available to get them,I've close the application and next time when I'll open the application want to check the status of messages, which is delivered or not.

回答1:

You can use XEP-0184: Message Delivery Receipts for check delivery of messages to destination. First you must add the gradle dependency of smack-extensions:

implementation 'org.igniterealtime.smack:smack-extensions:4.2.2;

Then use this code, when you want to send a message, to add receipt request to stanza:

DeliveryReceiptRequest.addTo(message);

Then you can receive the delivery status in a listener like this:

DeliveryReceiptManager d = DeliveryReceiptManager.getInstanceFor(connection);
d.addReceiptReceivedListener(new ReceiptReceivedListener() {
    @Override
    public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
        Log.i("delivery", "for: " + receiptId + " received");
        //here you can use sid or receiptId to identify which message is delivered
    }
});

Consider that when you are sending message, a random unique stanza-id (sid) will be setted to your stanza. you must save it in your message row in database, then you can identify that with this sid when receipt received.