I read some examples and tested them but all of them need to start a chat with someone first to receive Incoming Messages... I want to retrieve this Incoming Messages without need to talk first to the jid anyone can give an example ?
4 Answers
30
votes
You need to register a ChatListener to be notified of new chats, then you can add a message listener to them like normal:
connection.getChatManager().addChatListener(new ChatManagerListenerImpl());
....
private class ChatManagerListenerImpl implements ChatManagerListener {
/** {@inheritDoc} */
@Override
public void chatCreated(final Chat chat, final boolean createdLocally) {
chat.addMessageListener(...);
}
}
15
votes
i just wanted to add a copy & paste sample:
// connect to server
XMPPConnection connection = new XMPPConnection("jabber.org");
connection.connect();
connection.login("user", "password"); // TODO: change user and pass
// register listeners
ChatManager chatmanager = connection.getChatManager();
connection.getChatManager().addChatListener(new ChatManagerListener()
{
public void chatCreated(final Chat chat, final boolean createdLocally)
{
chat.addMessageListener(new MessageListener()
{
public void processMessage(Chat chat, Message message)
{
System.out.println("Received message: "
+ (message != null ? message.getBody() : "NULL"));
}
});
}
});
// idle for 20 seconds
final long start = System.nanoTime();
while ((System.nanoTime() - start) / 1000000 < 20000) // do for 20 seconds
{
Thread.sleep(500);
}
connection.disconnect();
This sample connects to jabber.org and displays every received message on the console.
9
votes
Please find the following code.
Please add smack.jar & smackx.jar to your build path
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
public class GoogleTalkDemo extends Thread{
private XMPPConnection xmppConnection;
public void connect(String server, int port, String s) throws Exception {
xmppConnection = new XMPPConnection(new ConnectionConfiguration(server, port,s));
xmppConnection.connect();
}
public void disconnect(){
if(xmppConnection != null){
xmppConnection.disconnect();
interrupt();
}
}
public void login(String username, String password) throws Exception{
connect("talk.google.com", 5222, "gmail.com");
xmppConnection.login(username, password);
}
public void run(){
try {
login("[email protected]", "your password");
System.out.println("Login successful");
listeningForMessages();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
GoogleTalkDemo gtd = new GoogleTalkDemo();
gtd.run();
}
public void listeningForMessages() {
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));
PacketCollector collector = xmppConnection.createPacketCollector(filter);
while (true) {
Packet packet = collector.nextResult();
if (packet instanceof Message) {
Message message = (Message) packet;
if (message != null && message.getBody() != null)
System.out.println("Received message from "
+ packet.getFrom() + " : "
+ (message != null ? message.getBody() : "NULL"));
}
}
}
}