简体   繁体   English

如何使用smack 4.1.8+在同一个监听器上监听单用户聊天和多用户聊天的传入消息

[英]how listen incoming message of single user chat and multi user chat on same listener using smack 4.1.8+

How to listen incoming messages for multi user chat and single user chat on same listener. 如何在同一个侦听器上收听多用户聊天和单个用户聊天的传入消息。 now for single user chat i am using following code: 现在单用户聊天我正在使用以下代码:

chatManager.addChatListener(new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally) {

    chat.addMessageListener(new ChatStateListener() {
        @Override
        public void stateChanged(Chat chat, ChatState state) {

            Log.d(TAG,"...stateChanged called...");

            if (ChatState.composing.equals(state)) {
                Log.d("Chat State",chat.getParticipant() + " is typing..");
            } else if (ChatState.gone.equals(state)) {
                Log.d("Chat State",chat.getParticipant() + " has left the conversation.");
            } else {
                Log.d("Chat State",chat.getParticipant() + ": " + state.name());
            }
        }

        @Override
        public void processMessage(Chat chat, Message message) {

            Log.d(TAG,"---Received a message---");
            Log.d(TAG,"participant: "+chat.getParticipant());
            Log.d(TAG,"message.getBody(): "+message.getBody());
            Log.d(TAG,"message.getFrom(): "+message.getFrom());
            Log.d(TAG,"message.getType(): "+message.getType());
            Log.d(TAG,"message.getSubject(): "+message.getSubject());

            String from=message.getFrom();
            String sender_id="";
            if(from.contains("/"))
            {
                sender_id=from.split("/")[0];
                Log.d(TAG,"The real sender_id is :" +sender_id);
            }
            else
            {
                sender_id=from;
            }

            Intent intent=new Intent(ChattingConnectionService.NEW_MESSAGE);
            intent.setPackage(mContext.getPackageName());
            intent.putExtra(ChattingConnectionService.BUNDLE_FROM_USERID,sender_id);
            intent.putExtra(ChattingConnectionService.BUNDLE_MESSAGE_BODY,message.getBody());
            mContext.sendBroadcast(intent);
            Log.d(TAG,"Received message from :"+sender_id+" broadcast sent.");
        }
    });

        }
    });

And for multi user chat i am using following code: 对于多用户聊天,我使用以下代码:

 multiUserChat=multiUserChatManager.getMultiUserChat(stringExtra);
    multiUserChat.addMessageListener(new MessageListener() {
        @Override
        public void processMessage(Message message) {
            Log.d(TAG,"---process message called in joinRoom method");
            Log.d(TAG,"---Received a message from: "+message.getFrom());
            Log.d(TAG,"message.getBody(): "+message.getBody());
            Log.d(TAG,"message.getFrom(): "+message.getFrom());
            Log.d(TAG,"message.getType(): "+message.getType());
            Log.d(TAG,"message.getSubject(): "+message.getSubject());

            if(message.getBody()!=null)
            {
                Intent intent=new Intent(ChattingConnectionService.NEW_MESSAGE);
                intent.setPackage(mContext.getPackageName());
                intent.putExtra(ChattingConnectionService.BUNDLE_FROM_USERID,message.getFrom());
                intent.putExtra(ChattingConnectionService.BUNDLE_MESSAGE_BODY,message.getBody());
                mContext.sendBroadcast(intent);
                Log.d(TAG,"Received message from :"+message.getFrom()+" broadcast sent.");
            }
        }
    });

I want to listen all these incoming messages on same listener? 我想在同一个监听器上收听所有这些传入的消息? please help me? 请帮我? I am on fire in office. 我在办公室里着火了。 Thanks in advance. 提前致谢。

I don't know how your infrastructure for the chat looks like, but anyhow I would recommend using the Firebase Cloud Messaging Api (former Google Cloud Messaging) 我不知道您的聊天基础设施是什么样的,但无论如何我建议使用Firebase云消息传递Api (以前的Google云消息传递)

When user A is sending a message to user B , when the server gets this message you can use the Firebase Cloud Messaging to send a message or payload to user B and notify the user that he/she has received a message. 当用户A向用户B发送消息时,当服务器收到此消息时,您可以使用Firebase Cloud Messaging向用户B发送消息或有效负载,并通知用户他/她已收到消息。

For a groupchats you can either use the solution above, when user A has sent a message to the groupchat workaholics your chat-server is querying all the recipients in that room, eg the users B and C . 对于组聊天,您可以使用上述解决方案,当用户A向群组聊天workaholics发送消息时,您的聊天服务器正在查询该会议室中的所有收件人,例如用户BC

Another tip for handling a listener for a groupchat would be to use the Topic Messaging feature from Firebase Cloud Messaging found here: 处理群聊的监听器的另一个技巧是使用Firebase Cloud Messaging中的Topic Messaging功能:

https://firebase.google.com/docs/cloud-messaging/android/topic-messaging https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

FCM (Firebase Cloud Messaging) is the way to go when you want to send notifications to specific devices or groups of devices. 当您想要向特定设备或设备组发送通知时,FCM(Firebase云消息传递)是您的选择。 Works for both Android, iOS and more.. 适用于Android,iOS等等..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM