Phoenix
Phoenix

Reputation: 238

How to add users to a roster in xmpp?

I have successfully created a user using the following code:

accountmanager = new org.jivesoftware.smack.AccountManager(connection);
accountmanager.createAccount(fbuserid,fbuserid);

But I am not able to add other users to the logged in user's roster using the following code :

 public void createEntry(String user, String name, String[] groups) throws XMPPException {
    // Create and send roster entry creation packet.
    RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.SET);
    RosterPacket.Item item = new RosterPacket.Item(user, name);
    if (groups != null) {
        for (String group : groups) {
            if (group != null) {
                item.addGroupName(group);
            }
        }
    }
    rosterPacket.addRosterItem(item);
    // Wait up to a certain number of seconds for a reply from the server.
    PacketCollector collector = connection.createPacketCollector(
            new PacketIDFilter(rosterPacket.getPacketID()));
    connection.sendPacket(rosterPacket);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }

    // Create a presence subscription packet and send.
    Presence presencePacket = new Presence(Presence.Type.subscribe);
    presencePacket.setTo(user);
    connection.sendPacket(presencePacket);
}

I am always getting the response as null. Someone please help me to solve this and Thanks in advance

Upvotes: 3

Views: 4347

Answers (1)

sanjay
sanjay

Reputation: 135

Rosters and presence use a permissions-based model where users must give permission before they are added to someone else's roster. This protects a user's privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry it will be in a pending state until the other user accepts your request.

If another user requests a presence subscription so they can add you to their roster, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:

Automatically accept all presence subscription requests. Automatically reject all presence subscription requests. Process presence subscription requests manually. The mode can be set using the Roster.setSubscriptionMode(Roster.SubscriptionMode) method. Simple clients normally use one of the automated subscription modes, while full-featured clients should manually process subscription requests and let the end-user accept or reject each request. If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of Presence.Type.subscribe.

Try this in code first to add user in roster as well as requesting the users permission.

    roster = Roster.getInstanceFor(connection);
            if (!roster.isLoaded())
                try {
                    roster.reloadAndWait();
                } catch (SmackException.NotLoggedInException e) {
                    Log.i(TAG, "NotLoggedInException");
                    e.printStackTrace();
                } catch (SmackException.NotConnectedException e) {
                    Log.i(TAG, "NotConnectedException");
                    e.printStackTrace();
                }
   roster.createEntry(jID, name, null);

On other user side/ in your code after login from one user:

    roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);

To make user status online:

    Presence presence = new Presence(Presence.Type.available);
            presence.setStatus("Online, Programmatically!");
            presence.setPriority(24);
            presence.setMode(Presence.Mode.available);
            roster = Roster.getInstanceFor(connection);

Upvotes: 7

Related Questions