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());
} }
}