[英]How to get IQ tag using smack Java?
实际上,问题是当我的 xmpp 客户端发送朋友邀请,然后接收方批准邀请时,openfire 服务器再次向发起方/邀请发送方推送订阅包进行授权,这就是为什么我想通过自动阻止这种情况使用 IQ 标签对其进行过滤,然后自动对其进行授权。
但是使用 PacketListener,我无法获得 IQ 标签...
我怎样才能做到这一点?
@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);
}
}
可以使用“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);
您可以使用 IQTypeFilter 来实现它,它是 IQ 数据包类型的过滤器:
public final class IQTypeFilter extends FlexibleStanzaTypeFilter<IQ> {
public static final StanzaFilter GET = new IQTypeFilter(Type.get);
public static final StanzaFilter SET = new IQTypeFilter(Type.set);
public static final StanzaFilter RESULT = new IQTypeFilter(Type.result);
public static final StanzaFilter ERROR = new IQTypeFilter(Type.error);
public static final StanzaFilter GET_OR_SET = new OrFilter(GET, SET);
private final IQ.Type type;
private IQTypeFilter(IQ.Type type) {
super(IQ.class);
this.type = Objects.requireNonNull(type, "Type must not be null");
}
@Override
protected boolean acceptSpecific(IQ iq) {
return iq.getType() == type;
}
@Override
public String toString() {
return getClass().getSimpleName() + ": type=" + type;
}
如 Javadoc 中所定义,IQTypeFilter 是 IQ 数据包类型的过滤器。 仅当数据包是 IQ 数据包并且它与构造函数中提供的类型匹配时才返回 true。 有一些使用IQTypeFilter的例子
这是使用Smack 4.3.4的更新答案。 我明确地发表了评论,以使我的代码清晰。
/**
* packet listener: listen for incoming messages of whith IQ as elelement
* exmaple of IQ : <iq from="mbula@domain" to="dedi@domain" type="get" >.......</iq>
*/
public static void listenToStanzas(AbstractXMPPConnection connection){
// IQ filter type. it can Be GET, SET, RESULT, ERROR
//in my case I filter SET IQs
StanzaFilter filter = IQTypeFilter.SET;
connection.addSyncStanzaListener(new StanzaListener() {
@Override
public void processStanza(Stanza packet) {
//Put yoour code Here
}
}, filter);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.