Hosein Kord
Hosein Kord

Reputation: 197

smack message sent status

I am using smack and openfire for create chat app in android . for message status I have no problem with delivered and displayed message in other client (double check). I will send a simple json message like bellow to sender: {"delivery":timestapmp} and parse it and double check messages with lower than timestamp that sent before. the problem is about sent status (one check). When i send message the server no response anything that message has sent . is it possible in smack to send message with callback from server. if possible and is it possible to send time server in callback response . thanks .

Upvotes: 3

Views: 2731

Answers (2)

Moulesh
Moulesh

Reputation: 2810

private void acknowledgementFromServer(final Message message) throws StreamManagementException.StreamManagementNotEnabledException {
        if (connection != null && connection.isSmEnabled()) {
            connection.addStanzaIdAcknowledgedListener(message.getStanzaId(), new StanzaListener() {
                @Override
                public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {

                        MessageAsyncTask task = new MessageAsyncTask(packet.getStanzaId(), MSG_STATUS_SENT);
                        task.execute();

                }
            });
        }

Hey you can do it like this.. call method every time you send message by passing that message as a parameter in above method

Note: Stream Management should be enabled for this to work, can be done like below:

DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
        ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
        ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());

Upvotes: 8

Raut Darpan
Raut Darpan

Reputation: 1530

According to my knowledge I have got up to this Inteface : ReceiptReceivedListener which is in smack 4.2

below is how I have implemented this :

private ReceiptReceivedListener receiptReceivedListener;

/**
 * get DeliveryReceiptManager
 *
 * @return
 */
private DeliveryReceiptManager getDeliveryReceiptManager() {
    if (deliveryReceiptManager == null && getConnection() != null) {
        deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(getConnection());
    }
    return deliveryReceiptManager;
}

add Listener

getDeliveryReceiptManager().addReceiptReceivedListener(receiptReceivedListener);

Received the call back

receiptReceivedListener = new ReceiptReceivedListener() {
    @Override
    public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
        //TODO : on recieved status of message delivery
    }
};

This will help you for sure

Below is the Interface for Smack 4.2 with full details :

/**
 * Callback invoked when a new receipt got received.
 * <p>
 * {@code receiptId} correspondents to the message ID, which can be obtained with
 * {@link org.jivesoftware.smack.packet.Stanza#getStanzaId()}.
 * </p>
 * 
 * @param fromJid the jid that send this receipt
 * @param toJid the jid which received this receipt
 * @param receiptId the message ID of the stanza(/packet) which has been received and this receipt is for
 * @param receipt the receipt
 */
void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt);

Upvotes: 2

Related Questions