0

我正在使用 smack 库开发聊天应用程序。我在群聊中有问题。在我的应用程序中,我正在创建组,并且成员是自动加入的。我想在我在组中发送消息时通知所有用户,即使他们没有发起聊天。我的代码如下,我在 init 方法中放置了监听器,但是无法接收消息。

 //Initialize
public void init(String userId, String pwd, Context context) throws SmackException.NotConnectedException {
    this.mUserName = userId;
    this.mPassWord = pwd;
    this.mContext = context;

    sessionManager = new SessionManager(context);

    if (userId.contains("@")) {
        this.mUserName = userId.split("@")[0];
    }
    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setUsernameAndPassword(mUserName, mPassWord);
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    configBuilder.setServiceName(XMPPCredential.SERVICE);
    configBuilder.setHost(XMPPCredential.HOST);
    configBuilder.setPort(XMPPCredential.PORT);
    configBuilder.setResource("");
//        configBuilder.setDebuggerEnabled(true);


    mConnection = new XMPPTCPConnection(configBuilder.build());
    PingManager pingManager = PingManager.getInstanceFor(mConnection);
    pingManager.setPingInterval(300); // 2.5 min
    pingManager.registerPingFailedListener(this);

    mChatmanager.getInstanceFor(mConnection).addChatListener(this);

    multiUserChatManager = MultiUserChatManager.getInstanceFor(mConnection);
    mConnection.addAsyncStanzaListener(this, null);
    mConnection.addSyncStanzaListener(this,null);

    ReconnectionManager.getInstanceFor(mConnection).enableAutomaticReconnection();
    mConnection.addConnectionListener(this);

    // Connect with XMPP server
    connectConnection(context);
}
4

1 回答 1

0

每个 MultiUserChat 都需要像这样添加一个 Listener:

MultiUserChat muc =  MultiUserChatManager.getInstanceFor(mConnection).getMultiUserChat( mucJid );

muc.addMessageListener(new MessageListener()...);
于 2016-10-19T09:11:20.910 回答