2012-03-18 108 views
1

實際上,問題是當我的xmpp客戶端發送好友邀請,然後收件人已批准邀請時,openfire服務器再次向發起者/邀請發送者推送要授權的訂閱數據包,這就是爲什麼我要防止這種情況通過使用IQ標籤自動進行過濾,然後自動對其進行授權。如何使用smack獲得IQ標籤?

但PacketListener,我不能讓智商的標籤......

我怎樣才能做到這一點?

@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); 
    } 
} 

回答

3

可以通過使用「IQTypeFilter」過濾器過濾輸入的IQ。這是說明該方法的示例代碼。

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); 
+0

工作太好了!謝謝! – user724861 2012-04-08 15:12:21

+0

您能否詳細說明您的答案。其實我不知道如何處理IQ消息 – 2014-03-10 11:00:12