实际上,问题是当我的 xmpp 客户端发送 friend 邀请,然后接收者批准了邀请,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);
关于java - 如何使用 smack Java 获取 IQ 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9759263/