smack 消息发送状态

smack message sent status

我正在使用 smack 和 openfire 在 android 中创建聊天应用程序。 对于消息状态,我在其他地方发送和显示的消息没有问题 客户(仔细检查)。 我将向发件人发送一条简单的 json 消息,如下所示: {"delivery":timestapmp} 并解析它并仔细检查低于之前发送的时间戳的消息。 问题是关于发送状态(一次检查)。 当我发送消息时,服务器没有响应消息发送的任何内容。 是否有可能在 smack 中发送带有服务器回调的消息。 如果可能,是否可以在回调响应中发送时间服务器。 谢谢.

据我所知,我已经达到了这个界面:ReceiptReceivedListener 位于 smack 4.2

以下是我的实现方式:

private ReceiptReceivedListener receiptReceivedListener;

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

添加监听器

getDeliveryReceiptManager().addReceiptReceivedListener(receiptReceivedListener);

收到回电

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

这肯定能帮到你

以下是 Smack 4.2 的界面,包含完整的详细信息:

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

                }
            });
        }

嘿,你可以这样做。每次发送消息时调用方法,方法是将消息作为上述方法中的参数传递

注意:流管理应该启用才能工作,可以像下面这样完成:

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