Conn.addPacketListener(new SubscriptionRequestPacketListener(), new PacketTypeFilter(Presence.class));
" You need to add this listener to the XMPPconnection before logging in[/i]:"
Why?
I have added the packetlistener after I login in, and I can deal with the subscribe after I login in, but I can’‘t deal(find) the subscribe before I login. What’’ the reason?
Process for Subscribe
Posted: Jan 17, 2005 2:52 PM in response to: AWenckus
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”);