3
votes

i'm using smack 4.1 library where i'm unable to revived message event.

here is my code-

 Chat chat = connection.getChatManager().createChat("user2@openfireserver", new MessageListener() {
            @Override
            public void processMessage(Chat arg0, Message arg1) {

                System.out.println(arg1.getBody());

            }
        });
        chat.sendMessage("test message");

debug report-

<message id="WrfOv-14" to="user1@openfireserver/Smack" from="user2@openfireserver/Smack" type="chat"><body>my test application</body><thread>56b1a75c-69a5-4a12-b12a-d24e072a2ce7</thread></message>

receive chat show in debug but not getting event in processMessage method. (may my question is duplicate but i'm not find relevant answer)

4

4 Answers

5
votes

I have used Smack 4.1 library in one of my Android projects. The following code snippet worked for me.

ChatManager.getInstanceFor(connection).addChatListener(new ChatManagerListener() {
            @Override
            public void chatCreated(Chat chat, boolean createdLocally) {
                chat.addMessageListener(new ChatMessageListener() {
                    @Override
                    public void processMessage(Chat chat, Message message) {
                        if (message.getType() == Message.Type.chat || message.getType() == Message.Type.normal) {
                            if(message.getBody()!=null) {
                              Toast.makeText(this,message.getFrom() + " : " + message.getBody(),Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });
            }
        });
0
votes

Here is an example for smack-4.1.0-beta3-SNAPSHOT-2015-02-09: It works for me, try it.

private void onAutthenticated() {
     ChatManager.getInstanceFor(mConnection).addChatListener(ChatConnection.this);
}


@Override
public void chatCreated(Chat chat, boolean b) {
    chat.addMessageListener(new ChatMessageListener() {
        @Override
        public void processMessage(Chat chat, Message message) {
              if (message.getType().equals(Message.Type.chat) || message.getType().equals(Message.Type.normal)) {
                  Log.d(TAG, message.getFrom());
                  Log.d(TAG, message.getBody());
              }
        }
    });
}
0
votes

For Receiving any Type of Message, you can use following code

StanzaTypeFilter message_filter = new StanzaTypeFilter(Message.class);
connection.addSyncStanzaListener(new StanzaListener() {
  @Override
  public void processPacket(Stanza packet) throws NotConnectedException {

   Message message = (Message)packet;
   if(message.getType() == Message.Type.chat) {
      //single chat message
   } else if(message.getType() == Message.Type.groupchat) {
      //group chat message
   } else if(message.getType() == Message.Type.error) {
      //error message
   }

  }
}, message_filter);
-2
votes

I think the create methord and the add methord are not same. create methord does not register a listener,but the add methord does register.When I see the api doc, I have the same question with you,but the doc does not give me an answer.So i guess this may be where the problem exists.