I am creating a XMPP Android chat client with asmack library and Openfire server.
I want to get the presence of all the roster from my roster list and display all online roster's, but when I check the Presence of roster it gives me unavailable for all the roster and status is null.
Below is my code how I am creating a roster and check for presence?
setting Presence
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("I’m available");
connection.sendPacket(presence);
Get Roster List and check for presence
Collection<RosterEntry> entries = roster.getEntries();
Log.e("Roster Entries","Roster Entries"+entries);
//loop through
for (RosterEntry entry : entries) {
//example: get presence, type, mode, status
Presence entryPresence = roster.getPresence(entry.getUser());
Presence.Type userType = entryPresence.getType();
Presence.Mode mode = entryPresence.getMode();
String status = entryPresence.getStatus();
int state = retrieveState_mode (entryPresence.getMode() ,entryPresence.isAvailable());
Log.e("Presence Status","Presence status "+entryPresence.toString()+" type "+userType+" mode "+mode+" status "+status+" state "+state);
}
retriveState_mode()
public static int retrieveState_mode(Presence.Mode userMode, boolean isOnline) {
int userState = 0;
/** 0 for offline, 1 for online, 2 for away,3 for busy*/
if(userMode == Presence.Mode.dnd) {
userState = 3;
} else if (userMode == Presence.Mode.away || userMode == Presence.Mode.xa) {
userState = 2;
} else if (isOnline) {
userState = 1;
}
return userState;
}
In log I get this:
Roster Entries﹕ Roster Entries[tusha: tusha, tushar: tushar]
Presence status unavailable type unavailable mode null status null state 0
Presence status unavailable type unavailable mode null status null state 0
As my experience before you can see status and other from Presence you need to subscribe the user.
for example: user A want to see status and available status from user B, in this case, user A need to subscribe user B. after that user A can see the user B Presence.
Subscribe Code
try {
roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
String userName = responders.getUsers().get(i).getUsername();
roster.createEntry("userB@domain", userName, null);
Presence pres = new Presence(Presence.Type.subscribe);
pres.setFrom("userA@domain");
connection.sendStanza(pres);
} catch (Exception e) {
android.util.Log.e("tag", "unable to add contact: ", e);
}