如何获得输入状态通知的XMPP如何获得、状态、通知、XMPP

2023-09-05 06:55:09 作者:星野绘里

我试图安装一个X​​MPP聊天应用程序。现在我能够到Gmail之间发送消息到Gmail。我想打字的通知,如用户输入的东西或用户输入,,, 的那样。我使用的是主类和消息监听器类。

I am trying to setup an XMPP chat application. Right now I am able to send messages between gmail to gmail. I want typing notification such as user typed something or user is typing,,, Like that. I am using a main class and a Message listener class.

主类

public class XMPPChatDemoActivity extends Activity {

    //Object of Custom class
    MessageListener messageListener = new MessageListenerImpl();

    private void setConnection(XMPPConnection connection) {
        this.connection = connection;

        ChatManager chatmanager = connection.getChatManager();
        Chat newChat = chatmanager.createChat("user@gmail.com", messageListener);
        try {
            newChat.sendMessage("Howdy!");
        }
        catch (XMPPException e) {
            System.out.println("Error Delivering block");
        }

消息监听器类

public class MessageListenerImpl implements MessageListener , ChatStateListener{

    @Override
    public void stateChanged(Chat arg0, ChatState arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void processMessage(Chat arg0, Message arg1) {
        // TODO Auto-generated method stub

        System.out.print("here");

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

    }

}

下面每当user@gmail.com开始输入一些东西在我们的谈话我需要在登录的消息。但其不来了。它发送消息。

Here whenever user@gmail.com starts to type something in our conversation I need to get a message in Log. But its not coming. It is sending messages..

我试图在堆栈溢出另一种答案。 How要知道在XMPP的Openfire打字状态使用啪

I tried another answer in stack-overflow. How to know Typing Status in XMPP openfire using Smack

但是从这个我也没有得到解决。请帮我。

But from this also I didn't get a solution. Please help me out.

推荐答案

我终于得到了解决。我需要使用聊天听众一起聊经理,也是我需要在建的的 sendcomposingnotification 的函数的使用。无需使用Messageeventrequestlistener接口或任何其它的自定义类。我加了以下几行,,

finally I got the solution. I need to use chat listener along with chat manager and also I need to use in built sendcomposingnotification function. No need to use Messageeventrequestlistener interface or any other custom class for this. I added the following lines,,

connection.getChatManager().addChatListener(new ChatManagerListener() {

            @Override
            public void chatCreated(final Chat arg0, final boolean arg1) {
                // TODO Auto-generated method stub

                arg0.addMessageListener(new MessageListener()
                {

                    @Override
                    public void processMessage(Chat arg0, Message arg1) 
                    {
                        // TODO Auto-generated method stub

                        Log.d("TYpe Stat",title[0] + " is typing......");
                        Toast.makeText(getApplicationContext(),title[0] + " is typing......",Toast.LENGTH_SHORT).show();
                    }



                    }
                }); 
            }
        });

和还需要发送通知这样的..

and also need to send notification like this..

mem.sendComposingNotification(etRecipient.getText().toString(), message.getPacketID());
        System.out.println("Sending notification");

其中,MEM被MessageEventManger的类型。 编号:http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/MessageEventManager.html

where mem is type of MessageEventManger. Ref: http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/MessageEventManager.html