0
votes

I am trying to create an Android chat client using ejabberd XMPP server (19.02), Smack library (4.2.4) and Android SDK 25 using Android Studio.

I followed the example app found here: https://www.blikoontech.com/tutorials/android-smack-xmpp-introductionbuilding-a-simple-client

All is working well and I can send messages between two different Android devices running that sample app.

In ejabberd, there are options to send messages to the clients directly from the server using a CLI tool called ejabberdctl or ejabberd REST API. When I sent messages that way, the Android client doesn’t receive those messages. I tried with other clients like Conversations and Gajim and they could all receive it. I am pretty sure messages sent using those methods arrived because they were received as offline messages (on ejabberd web admin) when sent to offline clients.

Here is the part of the Android (java) code (roosterconnection.java from that sample app) that is to receive incoming messages. Please suggest me if I am missing anything. Thanks a lot.

ChatManager.getInstanceFor(mConnection).addIncomingListener(new IncomingChatMessageListener() {
            @Override
            public void newIncomingMessage(EntityBareJid messageFrom, Message message, Chat chat) {
                ///ADDED
                Log.d(TAG,"message.getBody() :"+message.getBody());
                Log.d(TAG,"message.getFrom() :"+message.getFrom());

                String from = message.getFrom().toString();

                String contactJid="";
                if ( from.contains("/"))
                {
                    contactJid = from.split("/")[0];
                    Log.d(TAG,"The real jid is :" +contactJid);
                    Log.d(TAG,"The message is from :" +from);
                }else
                {
                    contactJid=from;
                }

                //Bundle up the intent and send the broadcast.
                Intent intent = new Intent(RoosterConnectionService.NEW_MESSAGE);
                intent.setPackage(mApplicationContext.getPackageName());
                intent.putExtra(RoosterConnectionService.BUNDLE_FROM_JID,contactJid);
                intent.putExtra(RoosterConnectionService.BUNDLE_MESSAGE_BODY,message.getBody());
                mApplicationContext.sendBroadcast(intent);
                Log.d(TAG,"Received message from :"+contactJid+" broadcast sent.");
                ///ADDED

            }
        });
1
If you send messages with ejabberdctl, your Android client don't receive them. What if you send the messages with any other client to your Android client?Badlop
Hi @Badlop, I tried that and my test Android client can receive messages sent from other clients. I have a feeling that the sample app I am following is an incomplete one and as such doesn't include the functionality to receive messages sent in "unconventional" ways. However, I studied the smack API docs and this - ChatManager.getInstanceFor(mConnection).addIncomingListener is the only way to listen for incoming messages.Phyo

1 Answers

0
votes

Here is a possible explanation, based in my experiments with a desktop client, Tkabber:

  1. I login to ejabberd using Tkabber client, account user1@localhost, resource tka1, priority -3. The negative priority in this experiment is important.

  2. Then I execute the command to send to full JID, including the correct resource:

ejabberdctl send_stanza aaa@localhost user1@localhost/tka1 "<message>..."

  1. The client receives the stanza correctly.

  2. Now I send to bare JID (without providing resource), and another setting another resource:

ejabberdctl send_stanza aaa@localhost user1@localhost "<message>..."

ejabberdctl send_stanza aaa@localhost user1@localhost/sdsd "<message>..."

  1. In those cases, none of them are received by the client, because the resource doesn't match, and because its priority is negative. I can see those messages stored offline in the database.

In your client, maybe you have to add another call to set the presence online, with a positive priority.