Search code examples
javaxmppsmack

How to get IQ tag using smack Java?


actually, the problem is when my xmpp client send a friend invitation, and then the recipient have Approve the invitation, the openfire server push again to the initiator/ invitation sender a subscription packet to be authorized, that's why I want to prevent this by automatically filter it using IQ tag and then automatically authorize it.

but with PacketListener, I can't get IQ tag...

How can I do this?

@Override
public void processPacket(Packet packet) {
    Log.i(TAG, "SECOND subscription");
    Log.d(TAG, "SECOND: "+packet.toXML());
    if (packet instanceof Presence) {
        Presence p = (Presence) packet;
        Log.d(TAG, "TYPE-Presence: "+p.getType());
        if (p.getType() != Presence.Type.subscribe)
        return;
        String from = p.getFrom();
        Log.d(TAG, "PACKET from: "+from);
        Notification notification = new Notification(android.R.drawable.stat_notify_more, mService.getString(
                R.string.AcceptContactRequest, from), System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        Intent intent = new Intent(mService, Subscription.class);
        intent.setData(Contact.makeXmppUri(from));
        notification.setLatestEventInfo(mService, from, mService
                .getString(R.string.AcceptContactRequestFrom, from), PendingIntent.getActivity(mService, 0,
                        intent, PendingIntent.FLAG_ONE_SHOT));
        int id = p.hashCode();
        mService.sendNotification(id, notification);
    }
}

Solution

  • The incoming IQs can be filtered out by using the "IQTypeFilter" filter. This is a sample code that illustrates the method.

        connection.connect();
    
        /* packet listener: listen for incoming messages of type IQ on the connection (whatever the buddy) */
        PacketFilter filter = new IQTypeFilter(IQ.Type.SET); // or IQ.Type.GET etc. according to what you like to filter. 
    
        connection.addPacketListener(new PacketListener() { 
            public void processPacket(Packet packet) {
                // HERE YOU PUT YOUR CODE TO HANDLE THE IQ MESSAGE
            }
        }, filter);