android 環信消息撤回

環信的消息撤回是要收費的,改成本地服務器保存並撤回消息:

消息 撤回:

原來的代碼:

case ContextMenuActivity.RESULT_CODE_RECALL://recall
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            EMMessage msgNotification = EMMessage.createTxtSendMessage(" ",contextMenuMessage.getTo());
                            EMTextMessageBody txtBody = new EMTextMessageBody(getResources().getString(R.string.msg_recall_by_self));
                            msgNotification.addBody(txtBody);
                            msgNotification.setMsgTime(contextMenuMessage.getMsgTime());
                            msgNotification.setLocalTime(contextMenuMessage.getMsgTime());
                            msgNotification.setAttribute(Constant.MESSAGE_TYPE_RECALL, true);
                            msgNotification.setStatus(EMMessage.Status.SUCCESS);
                            EMClient.getInstance().chatManager().recallMessage(contextMenuMessage);
                            EMClient.getInstance().chatManager().saveMessage(msgNotification);
                            messageList.refresh();
                            
                        } catch (final HyphenateException e) {
                            e.printStackTrace();
                            getActivity().runOnUiThread(new Runnable() {
                                public void run() {
                                    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();

改成:

case ContextMenuActivity.RESULT_CODE_RECALL://recall
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        EMMessage msgNotification = EMMessage.createTxtSendMessage(contextMenuMessage.getMsgId(),contextMenuMessage.getTo());
                        EMTextMessageBody txtBody = new EMTextMessageBody(getResources().getString(R.string.msg_recall_by_self));
                        msgNotification.addBody(txtBody);
                        msgNotification.setMsgTime(contextMenuMessage.getMsgTime());
                        msgNotification.setLocalTime(contextMenuMessage.getMsgTime());
                        msgNotification.setAttribute(Constant.MESSAGE_TYPE_RECALL, true);
                        msgNotification.setStatus(EMMessage.Status.SUCCESS);
                        recalDelMsg(contextMenuMessage.getMsgId(),msgNotification);
                    }
                }).start();
private void recalDelMsg(String msgId,EMMessage msgNotification) {
        HashMap<String, Object> baseParam = new HashMap<>(4);
        baseParam.put("msgId", msgId);
        OkHttpUtil.post(TAG, WebApi.MSN_CHAT_DEL, baseParam, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG,"recalDelMsg e="+e);
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG,"recalDelMsg response="+response);
                try {
                    JSONObject object = new JSONObject(response);
                    if (object.getInt("code") == 200) {
                        conversation.removeMessage(contextMenuMessage.getMsgId());
                        sendRecallMsg(msgNotification);
                        messageList.refresh();
//                        EaseDingMessageHelper.get().delete(contextMenuMessage);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

接收時:在DemoHelper.java的registerMessageListener()中onMessageReceived(List<EMMessage> messages)中添加代碼:

                try {
                        if (message.getBooleanAttribute(Constant.MESSAGE_TYPE_RECALL)) {
                            EMTextMessageBody textMessageBody = (EMTextMessageBody) message.getBody();
                            String msgId = textMessageBody.getMessage();
                            Log.e(TAG,"textMessageBody="+textMessageBody.getMessage());
                            Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
                            for (EMConversation conversation : conversations.values()) {
                                conversation.removeMessage(msgId);
                            }
                        }
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }

此種方法是把要刪除的消息 的msgId保存到提醒消息裏,所以如果對方在會話列表頁時,顯示的最後一條消息,是我要刪除的消息的msgId;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章