-1

As can see in asmack documentation when you receive one Chat type Message in processMessage() method from the MessageListener of Chat, I should to send back this message using Chat.sendMessage() to notify the message reception.

But in my chat implementation, I don´t use ChatManager when open one to one chat, I have an Service and Listener to do those things.

In Service I have an PacketListener to receive, and I am trying to do the send back message properly, as I can see in asmack documentation, but it don´t work.

this.xmppChat.addPacketListener(new PacketListener() {

            @Override
            public synchronized void processPacket(Packet packet) {

                Message message = (Message) packet;

                if (message.getType() == Message.Type.chat) {

                    Log.i("XMPP_SERVICE",
                            String.format("Mensaje recibido de %s",
                                    message.getFrom()));

                    // Rest of code implementation...

                    // At the end try to send back message to notify reception.
                    XMPPService.this.xmppChat.sendMessage(new Message(message.getBody()));
                }
            }
        }, new PacketTypeFilter(Message.class));

The XMPPService.this.xmppChat.sendMessage(Message message) method:

public boolean sendMessage(Message message) {

    Log.i("XMPP_INFO", "Enviando el mensaje");

    try {

        this.xmppManager.getConnection().sendPacket(message);

        Log.i("XMPP_INFO", "Enviado el mensaje");
        return true;
    } catch (Exception ex) {

        Log.e("XMPP_ERROR", ex.getMessage());
        return false;
    }
}

What wrong I am doing?, and How can I to fix it?

Best regards and thank you in advance.

Dani
  • 4,001
  • 7
  • 36
  • 60

2 Answers2

2

This is my code ,hope help to you, see it:

ChatManager cm = XmppTool.getConnection().getChatManager();


final Chat newchat = cm.createChat(userChat, new MessageListener() {

            @Override
            public void processMessage(Chat chat, Message message) {
                // TODO Auto-generated method stub
                Log.i(TAG,
                        "from:" + message.getFrom() + " body:"
                                + message.getBody());
                String[] args = new String[] { "hoafer", message.getBody(),
                        TimeRender.getDate(), "IN" };
            }
        });

If you want listen all message,you can use that code :

cm.addChatListener(new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean able) {
            chat.addMessageListener(new MessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) {
                    Log.i(TAG, "receiver message~!");
                    String tmp = message.getFrom();
                    String body=message.getBody();
                    String[] from = tmp.split("@");
                    Log.i(TAG, "from:" + message.getFrom() + " body:"
                            + body);
                    }

            });
        }
    });

It is recommended that you use the latest version of the library, the old version seems to have a problem . I encountered a similar problem, so use new version library solved it

hoafer
  • 106
  • 4
  • Can you please give us the new link :) – Muneeb Mirza Dec 21 '14 at 09:40
  • @hoafer i am facing a problem in sending and receiving message on android device from xmpp server using asmack, Can you come over this [link](http://chat.stackoverflow.com/rooms/68853/trying-to-learn) to help me, Thanks – nawaab saab Jan 19 '15 at 09:57
0

try this code may help you :

PacketFilter packetFilter = new MessageTypeFilter(
                        Message.Type.chat);

                connection.addPacketListener(new PacketListener() {
                    @Override
                    public void processPacket(Packet packet)
                            throws NotConnectedException {
                        Message message = (Message) packet;

                        if (message.getBody() != null) {
                            String fromName = StringUtils
                                    .parseBareAddress(message.getFrom());
                            Log.e("XMPPChatDemoActivity", "Text Recieved "
                                    + message.getBody() + " from "
                                    + fromName);

                            Packet received = new Message();
                            received.addExtension(new DeliveryReceipt(
                                    packet.getPacketID()));
                            received.setTo(packet.getFrom());
                            connection.sendPacket(received);


                        }

                    }
                }, packetFilter);
Vijju
  • 3,458
  • 1
  • 22
  • 20