asmack send message to a rouster group

2019-08-05 18:34发布

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?

标签: xmpp smack
2条回答
孤傲高冷的网名
2楼-- · 2019-08-05 18:47

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).

查看更多
The star\"
3楼-- · 2019-08-05 19:09

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.

查看更多
登录 后发表回答