Android smack server receipt ack

前端 未结 2 1778
暗喜
暗喜 2021-02-06 18:59

I\'m trying to make a chat application using XMPP. For this, I referred to this tutorial and successfully implemented it. But now I\'m unable to get that particular message is r

2条回答
  •  花落未央
    2021-02-06 19:58

    1. You need to save your messages by message id, you can get the id by message.getStanzaId().
    2. You have to enable stream management.

      static {
          XMPPTCPConnection.setUseStreamManagementDefault(true);
          XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
      }
      
    3. when you send out a mesage, you add an ack listener for that message, like this.

      try {
          if (mConnection.isSmEnabled()) {
              try {
                  mConnection.addStanzaIdAcknowledgedListener(message.getStanzaId(), new StanzaListener() {
                      @Override
                      public void processPacket(Stanza packet) throws NotConnectedException {
                          updateMessageStatus(packet);
                      }
                  });
              } catch (StreamManagementException.StreamManagementNotEnabledException e) {
                  e.printStackTrace();
              }
          }
          mConnection.sendStanza(message);
      } catch (NotConnectedException e) {
          e.printStackTrace();
      }
      

      Now, inside updateMessageStatus(packet) method, you find message in your database by id (packet.getStanzaId()) and update status from "pending" to "sent".

      Please take note that your server needs to enable stream management too.

提交回复
热议问题