I'm new to asmack and openfire, looked a lot for a working answer to this but couldn't find it anywhere. How do I retrieve offline messages on Logging into my account on asmack?
I've used the following code:
configure(ProviderManager.getInstance()); //configuring providers before creating a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT);
connConfig.setSendPresence(false);
connection = new XMPPConnection (connConfig);
try {
connection.connect();
} catch (XMPPException ex) {
setConnection(null);
}
try {
connection.login(username, password);
try {
OfflineMessageManager offlineManager = new OfflineMessageManager(
connection);
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager
.getMessages();
System.out.println(offlineManager.supportsFlexibleRetrieval());
System.out.println("Number of offline messages:: " + offlineManager.getMessageCount());
Map<String,ArrayList<Message>> offlineMsgs = new HashMap<String,ArrayList<Message>>();
while (it.hasNext()) {
org.jivesoftware.smack.packet.Message message = it.next();
System.out
.println("receive offline messages, the Received from [" + message.getFrom()
+ "] the message:" + message.getBody());
String fromUser = message.getFrom().split("/")[0];
if(offlineMsgs.containsKey(fromUser))
{
offlineMsgs.get(fromUser).add(message);
}else{
ArrayList<Message> temp = new ArrayList<Message>();
temp.add(message);
offlineMsgs.put(fromUser, temp);
}
}
// Deal with a collection of offline messages ...
offlineManager.deleteMessages();
} catch (Exception e) {
Log.e("CATCH","OFFLINE");
e.printStackTrace();
}
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);//Packet Listener
// Set the status to available
} catch (XMPPException ex) {
setConnection(null);
}
From what I've read, once a connection is established, Openfire automatically sends offline messages to the user. (if any) Which means that by just setting packet listeners after logging in, I should be able to retrieve the messages. However, this didn't work for me. Which's why I tried using OfflineMessageManager. It always shows 0 messages as the message count though. I even logged in to the mysql db which the server's using and checked the offline messages folder. The messages exist till the user logs in, which means that the messages are being sent but it isn't being retrieved by the app. I can't seem to find out how implement this. If anyone has a working solution, It'd be greatly appreciated.
i partially solved added a packet listener after login but the same problem persist when internet go down. in that case i intercept reconnectionSuccessful event and remove and add again the packet listener but the messages sent when the user was offline is lost .
Somebody have the best solution to solve it?
Send your presence afetr logging in to the XMPP server. You have forgot to add packetListener which listens for the upcoming offline messages.
Hope this works.
Related
I want to get chat history for one to one chat and group chat. after some research i came to know that i can do it by Message Archive Management . but it does not provide any example to get older message in to android client. i want to get older message chat history from server, but i dont know how to pull older messages from xmpp server. there are so many questions asked about this over here but didn't get any proper solution. what i have done till now is connection and sending message to other user. can anyone help me to get history of chat from xmpp server?
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("user1", "password")
.setHost(getString(R.string.domain))
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName(getString(R.string.domain))
.setPort(5222)
.setDebuggerEnabled(true) // to view what's happening in detail
.build();
XMPPTCPConnection conn1 = new XMPPTCPConnection(config);
conn1.setUseStreamManagement(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
conn1.setUseStreamManagementResumption(true);
// ReconnectionManager.getInstanceFor(this.conn1).enableAutomaticReconnection();
conn1.setPacketReplyTimeout(10000);
try {
conn1.connect();
if(conn1.isConnected()) {
Log.w("app", "conn done");
}
conn1.login();
if(conn1.isAuthenticated()) {
Log.w("app", "Auth done");
}
}
catch (Exception e) {
Log.w("app", e.toString());
}
ChatManager chatmanager = ChatManager.getInstanceFor(conn1);
Chat newChat = chatmanager.createChat("user2#"+getString(R.string.domain));
try {
newChat.sendMessage("Test Msg from user1!!!");
}
catch (SmackException.NotConnectedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'd use Smack's MamManager. For example MamManager.queryArchive(Integer).
Here's what I'm using:
Openfire 3.10.2
Smack 4.1.3
Environment: Android
I'm trying to search for a registered user, but I kept failing to do so. I've tried so many different combinations. I also tried on an older version of smack.
Here's my latest modified code:
UserSearchManager userSearchManager = new UserSearchManager(connection);
Form searchForm = null;
List<ReportedData.Row> list = null;
try {
userSearchManager.getSearchForm("search." + connection.getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", userName);
answerForm.setAnswer("Name", true);
answerForm.setAnswer("search", userName);
ReportedData data = userSearchManager.getSearchResults(answerForm, "search." + connection.getServiceName());
list = data.getRows();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
Log.e(LOG_TAG, e.getMessage());
} catch (SmackException.NotConnectedException e) {
Log.e(LOG_TAG, e.getMessage());
}
I would get this error
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: remote-server-not-found - cancel
on line,
Form searchForm = manager.getSearchForm("search." + connection.getServiceName());
I don't know if I missed configure something on openfire server, or do I need to setup something before I start searching.
My workaround idea was to get the full list of registered users and then search from there, but I'm not sure how to achieve that.
Once I logged in, the roster only consists of my friends and groups.
Could someone point me to the right direction?
You're trying to search for users on the server "search." + connection.getServiceName(), but your server is telling you that it can't find that server. It looks like you did not set up a search server in OpenFire, or you're using the wrong address.
as the title says I'm new to android.
I'm trying to build very simple chat with the following code.
//1. connection
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
try{ connection.connect();}
catch (XMPPException ex)
{
connection = null;
return;
}
//2. login
try{ connection.login(myGmailLogin,myGmailPwd); }
catch (XMPPException e)
{
report+="Login error " + e.toString() ;
return;
}
What i get Is: Login error SASL authentication failed using mechanism PLAIN.
By googling i've found dozens of pages from people having the same problem but no solution worked.
can anyone please help me?
thanx a lot!
Use
ConnectionConfiguration connConfig = new ConnectionConfiguration("gmail.com");,
all other params will be set automatically
Google does not allow PLAIN authentication on the insecure connection, make sure TLS connection is required: connConfig.setSecurityMode(SecurityMode.required);
I'm writing an Android app using sockets for communication. In a class called sever I accept clients (android devices) and open sockets for them.
Server side:
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Socket socket = serverSocket.accept();
Client clientThread = new Client(socket);
System.out.println("New client: " + clientThread.getName());
new Thread(clientThread).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
After a succesful connection the user may create a group, which is like a room for number of clients to connect and play together.
The group creation is done here:
Server side:
Client:
private void client_create() {
this.mGroup = new Group();
mGroup.joinPlayer(this);
System.out.println("New group for: " + name);
}
Group:
public Group(int nClients){
// Clients in this group
this.clients = new ArrayList<Client>();
}
public void joinPlayer(Client player){
clients.add(player);
}
Client Side:
Connection handling:
try {
socket = new Socket(hostName, portNumber);
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
Listener listener = new Listener();
new Thread(listener).start();
} catch (IOException e) {
e.printStackTrace();
}
I ran this programm on 2 android devices and on my localhost as the server. After the connection was made, I tried creating 2 independent different groups. While debugging it all seems legit until I reached to a point where I lost it due to the 2 different running threads.
The odd thing that happened is that after the first group was created with the first client (clients contains the first device client object), and then the second group with the second player (clients contains the second device client object), the first group clients array contains the second client object (from the second device).
Have you got any thoughts on that? Did I do something wrong?
Figured it out.
Clients was mistakly defined as static, so I guess when accessing to the clients array of the static object it received the last one who was created.
I m try to connect with Xmpp server,But i m getting exception
Login exception SASL authentication failed using mechanism DIGEST-MD5
i use this code ,can any one help me,or code
try {
if (xmppConnection == null) {
ConnectionConfiguration config = new ConnectionConfiguration(
SERVER_HOST, SERVER_PORT, SERVICE_NAME);
xmppConnection = new XMPPConnection(config);
System.out.println("xmppConnection"+xmppConnection);
}
if (!xmppConnection.isConnected()) {
xmppConnection.connect();
System.out.println("Connecting");
}
System.out.println("facebook id get xmpp "+username);
if (!xmppConnection.isAuthenticated()) {
xmppConnection.login(username, "123");
System.out.println("User is authenticated ");
}
Presence presence = new Presence(Presence.Type.available);
xmppConnection.sendPacket(presence);
} catch (Exception e) {
System.out.println("Login exception "+e);
e.printStackTrace();
}
In the Openfire configuration it is machinename.domain.com
This SASL mechanism uses also the Xmpp Domain name for authentication, not only username and password. This is why authentication fails.
mean your username & password must be like:
username: abc111#domain.com (whatever your domain name)
password: abcabc111
for more detail check this conversation.
I am using below code in my, its working fine in here..
try {
ConnectionConfiguration connConfig = new ConnectionConfiguration("HOST_IP", Integer.parseInt("PORT_NO"));
XMPPConnection connection = new XMPPConnection(connConfig);
connection.connect();
try {
// Login
connection.login("USER_NAME", "PASSWORD");
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
xmppClient.setConnection(connection);
} catch (XMPPException ex) {
Log.w("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
Log.w("XMPPClient", ex.toString());
xmppClient.setConnection(null);
}
} catch (XMPPException ex) {
Log.w("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
Log.w("XMPPClient", ex.toString());
xmppClient.setConnection(null);
}
And I have also added smack.jar file.
Please check below post, i think it might help you..
https://stackoverflow.com/a/6659403/1849482
And many users are getting this error in login. check below links for More Information..
http://community.igniterealtime.org/thread/44219
http://code.google.com/p/asmack/issues/detail?id=33
asmack uses Novell's Open LDAP DigestMD5SaslClient.java which does not support the escape of backslash ('\') and double-quote ('"') for SASL. According to XEP-0106, user name "user#company.com" should be encoded as "user\040company.com". Fully complying with SASL spec, it should be encoded as "user\\040company.com", but asmack didn't. If your XMPP server (e.g. Openfire) uses Java's SASL implementation, it will have a mismatch. Smack 3.x uses Java's SASL implementation, so it works fine. Smack 4.x is supposed to replace asmack, but I don't know if Smack 4.x uses Novell or Java SASL implementation.
BTW, this problem exists in iOS xmppframework as well.
If you are interested in my fix, I'll post it somewhere.
Here is the link to the revised DigestMD5SaslClient.java with the escape quoted string.