asmack send message to a rouster group

∥☆過路亽.° 提交于 2019-12-11 06:57:19

问题


I want to send a message to a roster Group (like friends group) by using asmack.(i dont want to create a room using multi user chat)

Asmach has this :

 Message msg=new Message(java.lang.String to, Message.Type type)

when want to a single user i use : Message("a@b.com",Message.Type.chat)

but in group chat I think I have to use Message(java.lang.String to, Message.Type.groupchat) but I don't know what java.lang.String to should be?


回答1:


XMPP does not specifiy a mechanism to send a message to a roster group. But you can easily implement that on your own. Just collect all the JIDs, and eventually all presences, of the roster group and send the message to every one of them (preferably with Smack's MultipleRecpientManager).




回答2:


I managed to send message to all the roster group members in following way. You can try it.


The method that sends message to multi-users in a group

/**
 * Sends Group message
 *
 * @param message
 * @param groupName
 * @throws XMPPException
 * @throws SmackException.NotConnectedException
 */
public void sendGroupMessage(String groupName, String message) throws SmackException.NotConnectedException, XMPPException {
    Roster roster = connection.getRoster();
    RosterGroup rosterGroup = roster.getGroup(groupName);
    Collection<RosterEntry> entries = rosterGroup.getEntries();
    for (RosterEntry entry : entries) {
       String user = entry.getName();
       System.out.println(String.format("Sending message " + message + " to user " + user));
        Chat chat = chatManager.createChat(user, messageListener);
        chat.sendMessage(message);
    }
}

The calling method

classNameOfYourMethod.sendGroupMessage(groupName, groupMessage);

if you need classification ask in comment.



来源:https://stackoverflow.com/questions/27577909/asmack-send-message-to-a-rouster-group

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