powered by Jive Software

Smack 4.2 listener to receive online and offline messages


#1

Thanks to everyone, I apologize for my English (I’m Italian)

I’m new to xmpp application development,

My intention is to create a client that provides Rest services to hybrid apps made with ionic framework.

I have been able to send messages to another user (tested by my client to send to a spark-connected user), opens the private chat and the message is sent, only that is reported as a message to everyone …

Also I did not understand how to receive messages, or rather how to implement listeners while offline, while offline, the examples found are for the old versions that I now see are deprecated,

Here’s an example of my code:

public class XMPPChatManager {   private ChatManager chatManager;   private String senderUser;   public XMPPChatManager(XMPPConnection connection , String user){
  this.chatManager = ChatManager.getInstanceFor(connection);
  this.chatManager.addIncomingListener(new XMPPIncomingListener());
  this.senderUser = user;
  }   public void sendMessage(String messageBody,String nameProperty , Object value){
  EntityBareJid jid = getFromEntity();
  if(jid == null) return;
  Chat chat = chatManager.chatWith(jid);
  Message message = new Message();
  message.setBody(messageBody);
  JivePropertiesManager.addProperty(message,nameProperty,value);
  try {
  chat.send(message);
  } catch (NotConnectedException | InterruptedException e) {
  e.printStackTrace();
  }
  }   private EntityBareJid getFromEntity(){
  try {
  return JidCreate.entityBareFrom(senderUser);
  } catch (XmppStringprepException e) {
  e.printStackTrace();
  return null;
  }
  } }
public class XMPPConnectionManager {
    private String host;     private int port;
    private String user;
    private String password;
    private AbstractXMPPConnection connection;
    private boolean connected = false;
    private boolean logged = false;     public XMPPConnectionManager (String host,int port, String user, String password){          this.host=host;
        this.port=port;
        this.user=user;
        this.password = password;
    }           public void connect(String serviceName) {
    if(!isConnected()){
      try {
  XMPPTCPConnectionConfiguration config  = XMPPTCPConnectionConfiguration.builder()
  .setUsernameAndPassword(user,password)
  .setXmppDomain("192.168.252.75")
  .setCompressionEnabled(false)
  .setHost(host)
  .setPort(port)   .setSecurityMode(SecurityMode.disabled)
  .setSendPresence(true)   .build();
  connection =  new XMPPTCPConnection(config);
  } catch (XmppStringprepException e) {
  e.printStackTrace();
  }
      try {
  connection.connect();
  setConnected(true);
  } catch (SmackException | IOException | XMPPException | InterruptedException e) {
  e.printStackTrace();
  }
    }
    }          public AbstractXMPPConnection getConnection() {
  return connection;
  }         public void disconnect(){
    if(isConnected()){
    connection.disconnect();
    setConnected(false);
    setLogged(false);
    }
       }
            public void login(){
    if(isConnected() && !isLogged())
  try {
  connection.login();
  setLogged(true);
  } catch (XMPPException | SmackException | IOException | InterruptedException e) {
  e.printStackTrace();
  }
    }         public boolean isConnected() {
  return connected;
  }
        public boolean isLogged() {
  return logged;
  }
            private void setLogged(boolean logged) {
    this.logged = logged;
    }
        private void setConnected(boolean connected) {
  this.connected = connected;
  } }
public class XMPPIncomingListener implements IncomingChatMessageListener{
  @Override
  public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
    System.out.println("MESSAGE from  : "+ message.getFrom().toString());
    System.out.println("MESSAGE TO: "+message.getTo().toString());
    System.out.println("BODY : "+message.getBody());
  } }
}

#2

For offline messages you can use the OfflineMessageManager.

Sample code:

OfflineMessageManager mOfflineMessageManager =
                new OfflineMessageManager(mConnection);

// Get the message size
int size = mOfflineMessageManager.getMessageCount();

// Load all messages from the storage
List<Message> messages = mOfflineMessageManager.getMessages();

NOTE: Make sure to set the “sendPresence(true)” to “false”, because if it´s set to true, you get all the offline messages and the app do not catch them.
After you received all messages from the storage you can set the presence to available.


#3

Hi,

I get all my messages as offline messages even when the receiver user is online. Hence I do not receive anything in the newIncomingMessage(…) method.

Could you please help me regarding this?

Thanks.


#4

When you set the XMPPTCPConnectionConfiguration, you must call this function “.setSendPresence(true)”.
Then you are able to receive the messages directly through the “newIncomingMessage(…)” method.

You can set the presence after you downloaded the offline messages from the storage.

Hope this helps.


#5

I found my mistake yesterday.
I was using “.setSendPresence(true)” and a few lines after that, I was setting it to false.

Anyway, Thanks for the prompt reply :slight_smile:


#6

Hi, I am getting message count but no message body.
Here a blank array is returning.

I am using smack-4.2.4
please help.