I built an im application and can succesfully chat with an other user.(with Xmpp,Smack). Now I wanted to get presence updates( when a user becomes online or offline (I do not care about the rest)) so I tried to apply the same code I used for messages to presences.(We have custom iqs we do not use the Roster of other classes)
public void listeningForMessages() {
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));
PacketCollector collector = mConnection.createPacketCollector(filter);
while (true) {
Packet packet = collector.nextResult();
if (packet instanceof Presence) {
Presence newPresence = (Presence) packet;
String fromPresence = newPresence.getFrom();
String contactJidPresence = "";
if (fromPresence.contains("/")) {
contactJidPresence = fromPresence.split("/")[0];
Log.d(TAG, "The real jid is :" + contactJidPresence);
} else {
contactJidPresence = fromPresence;
}
if (newPresence != null) {
System.out.println("Received availability update from "
+ contactJidPresence + " : "
+ (newPresence != null ? (packet) : "NULL"));
Handler handler1 = new Handler(Looper.getMainLooper());
toastThis = contactJidPresence;
handler1.post(new Runnable() {
@Override
public void run() {
//Your UI code here
if (!ChatActivity.active) {
Toast.makeText(mApplicationContext, " You received a presence from " + toastThis, Toast.LENGTH_LONG).show();
}
}
});
}
}
//Rest of the logic
}
}
I get the messages with the same way in the same while(true) statement and I can get them.But I do not get any presence updates(they are added to my "customroster" in server-side). For your conveinence I ll also copy how I get the messages but they are same.
if (packet instanceof Message) {
Message message = (Message) packet;
String from = message.getFrom();
String contactJid = "";
if (from.contains("/")) {
contactJid = from.split("/")[0];
Log.d(TAG, "The real jid is :" + contactJid);
} else {
contactJid = from;
}
if (message != null && message.getBody() != null) {
System.out.println("Received message from "
+ contactJid + " : "
+ (message != null ? message.getBody() : "NULL"));
Now after that I also send an intent with this but it doesnt matter in my question(I think because I dont even get a Presence packet so it never goes inside if statement)
Intent intent = new Intent(ConnectionService.NEW_MESSAGE);
intent.setPackage(mApplicationContext.getPackageName());
intent.putExtra(ConnectionService.BUNDLE_FROM_JID, contactJid);
intent.putExtra(ConnectionService.BUNDLE_MESSAGE_BODY, message.getBody());
mApplicationContext.sendBroadcast(intent);