Delete a Friend from Roster in Xmpp/Openfire

不打扰是莪最后的温柔 提交于 2019-12-22 01:07:43

问题


In my chat application. I am using smack library , with the help of Subscription Management I have done the part of adding a friend to the Roster of a particular person.

Now I want that when some person denies the friend request, I send a UNSUBSCRIBE PACKET to the other user for the same, the friend is not deleted from the roster of the other user. It simply shows NONE subscription.

CODE:

Presence unsubscribe = new Presence(Presence.Type.unsubscribe);
                unsubscribe.setTo(ABC@ABC.COM);             
                connection.sendPacket(unsubscribe);

How can I delete the user from the Roster of the friend. I can do it from openfire portal but don't know how to do it from code.


回答1:


From the Smack forum, this code might work:

RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.SET);
RosterPacket.Item item  = new RosterPacket.Item("ABC@ABC.COM", null);
item.setItemType(RosterPacket.ItemType.REMOVE);
packet.addRosterItem(item);
connection.sendPacket(packet);



回答2:


This code worked for me

    if(selectedRoster != null) {
        Presence presence = new Presence(Presence.Type.unsubscribe);
        presence.setTo(selectedRoster.getUser());
        presence.setStatus("Offline");
        presence.setShow("unavailable");
        ConnectionController.GetInstance(this).getXMPPConnection().sendPacket(presence);

        try {
            roster.removeEntry(selectedRoster);
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


来源:https://stackoverflow.com/questions/14317580/delete-a-friend-from-roster-in-xmpp-openfire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!