smack message sent status

老子叫甜甜 提交于 2019-12-06 07:59:31

问题


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 .


回答1:


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());



回答2:


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);


来源:https://stackoverflow.com/questions/44213180/smack-message-sent-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!