如何从openfire获取一对一聊天的聊天记录

时间:2017-02-27 12:41:16

标签: android openfire smack

有两个用户 A B

  • 首先登录, B 离线。
  • A B 发送消息。
  • 现在 B 在线,但无法收到 A 的消息 已发送至 B
  • 如果 A B ,则一次只能登录不同的设备 两者都在聊天然后消息发送和接收完成 完美。

帮我了解一对一聊天的聊天记录?

这是发送消息:

public void sendTextMessage(View v) {
        String message = msg_edittext.getEditableText().toString();
        if (!message.equalsIgnoreCase("")) {
            final ChatMessage chatMessage = new ChatMessage(user1, user2,
                    message, "" + random.nextInt(1000), false);
            chatMessage.setMsgID();
            chatMessage.body = message;
            chatMessage.Date = CommonMethods.getCurrentDate();
            chatMessage.Time = CommonMethods.getCurrentTime();
            msg_edittext.setText("");
            chatAdapter.add(chatMessage);
            chatAdapter.notifyDataSetChanged();
            //MainActivity activity = ((MainActivity) context);
            getmService().xmpp.sendMessage(chatMessage);
        }
    }

public void sendMessage(ChatMessage chatMessage)
    {
        String body = gson.toJson(chatMessage);
        if (!chat_created)
        {
            Mychat = ChatManager.getInstanceFor(connection).createChat(
                    chatMessage.receiver + "@"
                            + "sspl163",
                    mMessageListener);
            chat_created = true;
        }

        final Message message = new Message();
        message.setBody(body);
        message.setStanzaId(chatMessage.msgid);
        message.setType(Message.Type.chat);

        try {
            if (connection.isAuthenticated())
                Mychat.sendMessage(message);
            else
                login();
        }
        catch (NotConnectedException e) {
            Log.e("xmpp.SendMessage()", "msg Not sent!-Not Connected!");

        }
        catch (Exception e) {}
    }

这是用于检索消息:

private class MMessageListener implements ChatMessageListener
    {

        public MMessageListener(Context contxt) {}

        @Override
        public void processMessage(final org.jivesoftware.smack.chat.Chat chat, final Message message)
        {
            if (message.getType() == Message.Type.chat && message.getBody() != null)
            {
                final ChatMessage chatMessage = gson.fromJson(message.getBody(), ChatMessage.class);
                processMessage(chatMessage);
            }
        }

        private void processMessage(final ChatMessage chatMessage)
        {
            chatMessage.isMine = false;
            SharedPreferences shared = context.getSharedPreferences("MyPREFERENCES", MODE_PRIVATE);
            String user = (shared.getString("username", ""));
            if(chatMessage.receiver.equalsIgnoreCase(user) && Chats.user2.equalsIgnoreCase(chatMessage.sender))
                Chats.chatlist.add(chatMessage);
            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    Chats.chatAdapter.notifyDataSetChanged();
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

首先从网络管理员检查您的openfire设置

来自

服务器 - >服务器设置 - >离线消息并检查您的设置

对我而言下班。

enter image description here