powered by Jive Software

Process for Subscribe

Could someone please help me out on how to manually recieve subscriptions? I am having some problems with it. Please, help me out with what you would do when both recieving a subscription from someone else and adding a contact. thanks.

Ok,

First, at the beginnig of your code (before you connect to the server):

Roster.setDefaultSubscriptionMode(Roster.SUBSCRIPTION_MANUAL);

You need to have a packet listen class like this:

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.util.StringUtils; /** Shows a SubscriptionRequestDialog when a subscript request Presence packet is recieved.
* Shows an infomation pane when a subscribed packet is recieved*/
public final class SubscriptionRequestPacketListener implements PacketListener{
     
     public void processPacket(Packet packet) {
          Presence p;
          try{
               p=(Presence) packet;
          }
          catch (ClassCastException cce){
               return;
          }
          if(p.getType()==Presence.Type.SUBSCRIBE){
               // show a subscription request dialog
                       // snip
               return;
          }
          if(p.getType()==Presence.Type.SUBSCRIBED){
               // Tell the user someone has subscribed them and they can see that persons presence.
                        // snip
               return;
          }
          if(p.getType()==Presence.Type.UNSUBSCRIBE){
               Presence np=new Presence(Presence.Type.UNSUBSCRIBED);
               np.setTo(p.getFrom());
               np.setFrom(Conn.getUser());
               Conn.sendPacket(np);
          }
     }
}

You need to add this listener to the XMPPconnection before logging in:

Conn.addPacketListener(new SubscriptionRequestPacketListener(), new PacketTypeFilter(Presence.class));
Conn.login("username","password");

When the user wants to allow someone to see their presence, send this:

// accept the subscription request
Presence p=new Presence(Presence.Type.SUBSCRIBED);
try{
    p.setFrom(Conn.getUser());
    p.setTo(From); // the jabber id of the user requesting presence subscription
    Conn.sendPacket(p);
}
catch(Exception e){
    //show error msg to user
}

HTH,

Pheet

1 Like