Add Contact problem (in Presence Listener)

Hi ,

~at first, pardon me for my influent english writing

I get stuck implementing small client chat program T_T
Here’s the thing: userA & userB are already registered in jabber server. userA wants to add userB to be one of his contact list and wants userB to approve userA’s contact invitation. AFAIK, we need to send 2 kind of packets: RosterPacket and Presence. the RosterPacket is not a problem, it works (roster list for each user is updated correcltly). But the presence, i really have no idea . I’m convinced that if we send Presence packet to the server that notifying userA needs userB to approve userA’s invitation, then (when userB log-in) userB will receive invitation of notification from userA which userB needs to approve/disapprove.
In fact, userB doesn’t get any notification of invitation at all. In source code view, the PresenceListener (see code below) doesn’t get any incoming packet.

anyway, these are my source code which implement this feature:

//add to contact(roster) list
RosterPacket.Item rosterItem = new RosterPacket.Item(userAccount,usernameOnly);
rosterItem.addGroupName(“Default Friend”);

//send the RosterPacket to update roster list in jabber database
RosterPacket rosterPacket = new RosterPacket();

//send Presence packet to targeted user to get their approval.
Presence presence = new Presence(Presence.Type.subscribe);
String destination = connection.getUser();


  • Listen to check whether any Presence packet coming or not
    PresenceListener packetPresenceListener = new PresenceListener();
    PacketFilter packetFilter = new PacketTypeFilter(Presence.class);
    connection.addPacketListener(packetPresenceListener, packetFilter);


  • Presence Listener class --> listen to Presence packet only
    class PresenceListener implements PacketListener{
public void processPacket(Packet incomingPacket) {
    if(incomingPacket instanceof Presence){
        Presence thisPresence = (Presence) incomingPacket;
        System.out.println(thisPresence.getTo()+" has invited you ("+thisPresence.getFrom()+") to authorize his/her request or not");
        System.out.println("Presence status: "+thisPresence.getType()+"; Presence mode: "+thisPresence.getMode());


Well, can anyone help me what’s wrong with my source code? or anything that i miss?



Sometimes this problem happens because you aren’t adding your presence listener soon enough, so the invitation is arriving to your client before your listener is registered and then you miss the packet. Try turning on the Smack debugger to see whether the invitation packet is actually being sent to your client.

Also, there is a much easier way to establish a subscription than the code you posted. Just use the method Roster#createEntry and it will setup the appropriate packet and send it for you automatically. Just make sure you pass in the user’s full ID “user@domain” when you create the entry for them.


Anyway, this problem has been solved.

The thing is I always try test this add contact with the same 2 users (userA and userB) over and over. So I ended up with no notification whatsoever.

But when i test with 2 new users (userC and userD). The packet Presence & Its Subscription is sent.

I just figured out that createEntry method actually sends the subscription status.

Thx Chris