smack- create entry in roster
Asked Answered
O

1

6

I know that this question has been asked before, but only a partial response has been given by mschonaker here. On my website, user can add people to their roster, then the buddy has to accept and finally they are connected. The first person (user a) use the famous

roster.createEntry(jid, name, groups);

which works and add an entry in his roster, but then I am a bit confused to what to do:

  • how do I receive the request on the other end? I tried implementing a PacketListener, override processPacket() and check for packet which types are Presence.Type.subscribe or Presence.Type.subscribed, but it appears that it is only triggered for the user a, but not the one that should listen for subscriptions- user b.

  • then, I have another function that can lookup all requests at login, so if I login again I will see the request, but how do I accept it? at first, I thought that the user b should also add user a in his roster by roster.createEntry(jid, name, groups);

but that didn't work and nothing was happening. I also tried to do

Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(jid);
xMPPConnection.sendPacket(subscribed);

but didn't work either. I'm sure there must be a good and simple way to do it, but I haven't found it so far anywhere, and trying one thing at a time gave me too many headaches. Does anyone know the correct flow for this? thanks in advance!

Overweary answered 11/7, 2011 at 14:16 Comment(1)
I am facing the same problem can you please help regarding the sameGittel
D
4

From the Smack documentation: 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.

So, try setting Roster's subscription mode to manual, and then implement PacketListener to listen for Presence.Type.subscribe. Once a packet is received, create a new Packet with Presence.Type.subscribed and send it to the sender.

Demoiselle answered 13/7, 2011 at 14:21 Comment(3)
Hum I didn't set the mode to manual, that's probably why, stupid me. I thought it didn't really matter since by not doing so it was not accepting automatically the requests. Will try this nowOverweary
so I managed to get it work, but it is a bit messy, I wonder if there is a simpler way: user a add user b to roster, user b receive request and accept (this does not add user a to roster b), then user b add user a to his roster, and user a needs to accept (why? since he added b in the first place), then it seems to work ...Overweary
Yes, that's how it should be done. Have a look at the rfc: xmpp.org/rfcs/rfc3921.html#int , section 8.2., especially the last paragraph: Upon receiving the presence stanza of type "subscribed", the user SHOULD acknowledge receipt of that subscription state notification through either "affirming" it by sending a presence stanza of type "subscribe" to the contact or "denying" it by sending a presence stanza of type "unsubscribe" to the contact.Demoiselle

© 2022 - 2024 — McMap. All rights reserved.