Touchstone
Touchstone

Reputation: 5982

roster.getPresence gives unavailable

I am using smack api.

        Presence presencePacket = new Presence(Presence.Type.subscribe);
        presencePacket.setTo("[email protected]");
        conn1.sendPacket(presencePacket);

        Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
        Roster roster=conn1.getRoster();

        Collection<RosterEntry> entries = roster.getEntries();

        for (RosterEntry entry : entries) {
            System.out.println(roster.getPresence(entry.getUser()));
        }

In the above code roster.getPresence(entry.getUser()) gives status as unavailable

In addition, I have following queries:

  1. Here [email protected] is email id of the user whose username is danmorgan. Am I using the correct jid?
  2. If the above jid is wrong, then what could be the correct jid? Btw I have tried the following jid too: danmorgan@saint-pc.

Please take a note, that at openfire server i have set subscription mode as both for both the user.

Upvotes: 1

Views: 534

Answers (2)

Touchstone
Touchstone

Reputation: 5982

JID is an indispensable part.

Always use JID to login. The JID is combination of username and server name. So, in my case the JID would be username@server-name i.e dhmohn@saint-pc as the username is dhmohn and server name is saint-pc.

Please take a note, if you login with the following code then your roster will always be empty:

conn1.login("dhmohn", "Dhmohn123");

Rather, always login with the jabber ID:

conn1.login("dhmohn@saint-pc", "Dhmohn123");

After logging in, put the thread to sleep for 10 seconds. Later fetch the roster.

One could find the server name in admin console of Openfire:

server tab ->server manager->server informaton->server properties

Upvotes: 1

Rajan Bhavsar
Rajan Bhavsar

Reputation: 1867

Presence presence = new Presence(Presence.Type.available); connection.sendPacket(presence);

                Roster roster = connection.getRoster();
                roster.addRosterListener(new RosterListener() {

                    @Override
                    public void presenceChanged(Presence presence) {
                        // TODO Auto-generated method stub
                        String user = presence.getFrom();
                        // Presence bestPresence = roster.getPresence(user);
                        Log.e("User Presence--------->****-->",
                                "User Name-----" + user
                                        + "Online or Offline ---->"
                                        + presence.isAvailable());
                    }

                    @Override
                    public void entriesUpdated(Collection<String> arg0) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void entriesDeleted(Collection<String> arg0) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void entriesAdded(Collection<String> arg0) {
                        // TODO Auto-generated method stub

                    }
                });

Here You can get update of Presence Of User when he is going to Offline and online when Presence Changed.

Upvotes: 1

Related Questions