im即时通讯:android:消息

消息

发送文本、语音、图片、位置等消息(单聊/群聊通用)


     /**
     * 创建文本消息
     * @param content        文本内容
     * @param toChatUserName 发送给谁
     */
      DTMessage txtMessage = DTMessage.createTxtMessage(content, toChatusername);
    
            if (txtMessage == null) return;
            
            //发送消息类型(单聊、群聊)
            txtMessage.setChatType(DTConstant.ChatType.SINGLE);
            
            //消息状态监听 
            txtMessage.setMessageStatusCallBack(new DTMessageStatusCallBack);
            
            //发送消息
            DTClient.getInstance().msgManage().sendMessage(txtMessage);
     /**
     * 创建语音消息
     *
     * @param voicePath      语音路径
     * @param voiceLength    语音长度
     * @param toChatUserName 发给谁
     */
      DTMessage voiceMessage = DTMessage.createVoiceMessage(voicePath, voiceLength,toChatusername);
  
        if (voiceMessage == null) return;
        
         //发送消息类型(单聊、群聊)
        voiceMessage.setChatType(DTConstant.ChatType.SINGLE);
        
         //消息状态监听 
        voiceMessage.setMessageStatusCallBack(new DTMessageStatusCallBack);
        
        //发送消息
        DTClient.getInstance().msgManage().sendMessage(voiceMessage);
     /**
     * 创建图片消息
     * @param imagePath      图片路径
     * @param toChatUserName 发给谁
     */
       DTMessage imgMessage = DTMessage.createImageMessage(imgPath, contact.getUsername());
  
        if (imgMessage == null) return;
        
          //发送消息类型(单聊、群聊)
        imgMessage.setChatType(DTConstant.ChatType.SINGLE);
        
        //消息状态监听
        imgMessage.setMessageStatusCallBack(new DTMessageStatusCallBack);
        
        //发送消息
        DTClient.getInstance().msgManage().sendMessage(imgMessage);
        
     /**
     * 创建视频消息
     *
     * @param videoPath      视频路径
     * @param videoName      视频名
     * @param videoLength    视频长度
     * @param toChatUserName 发给谁
     */
     DTMessage videoMessage = DTMessage.createVideoMessage(videoPath, videoName, videoLength, contact.getUsername());
     
        if (videoMessage == null) return;
        
        //发送消息类型(单聊、群聊)
        videoMessage.setChatType(DTConstant.ChatType.SINGLE);
        
        //消息状态监听
        videoMessage.setMessageStatusCallBack(new DTMessageStatusCallBack);
        
        //发送消息
        DTClient.getInstance().msgManage().sendMessage(videoMessage);
     /**
     * 创建位置消息
     * @param locationAddress 具体位置
     * @param latitude        纬度
     * @param longitude       经度
     * @param toChatUserName  发给谁
     */
        DTMessage locationMessage = DTMessage.createLocationMessage(locationAddress, latitude, longitude,
                contact.getUsername());
                
        if (locationMessage == null) return;
        
        //发送消息类型(单聊、群聊)
        locationMessage.setChatType(DTConstant.ChatType.SINGLE);
        
        //消息状态监听
        locationMessage.setMessageStatusCallBack(new DTMessageStatusCallBack);
        
        //发送消息 
        DTClient.getInstance().msgManage().sendMessage(locationMessage);
     /**
     * 发送文件消息
     * @param fileName       文件名称
     * @param filePath       文件路径
     * @param toChatUserName 发给谁
     */
     DTMessage fileMessage = DTMessage.createFileMessage(fileName, filePath, contact.getUsername());
  
        if (fileMessage == null) return;      
      
        //发送消息类型(单聊、群聊)
        fileMessage.setChatType(itemType);
        
        //消息状态监听
        fileMessage.setMessageStatusCallBack(new DTMessageStatusCallBack);
        
        //发送消息
        DTClient.getInstance().msgManage().sendMessage(fileMessage);
        

通过注册消息监听来接收消息

//设置接收消息监听
DTClient.getInstance().msgManage().addMessageListener(msgListener , toChatUsername );

DTMessageListener  msgListener = new DTMessageListener {


    /**
     * 收到消息
     */
    void onMessageReceived(DTMessage msg);

    /**
     * 收到已读回执
     */
    void onMessageRead(DTMessage msg);

    /**
     * 收到消息送达
     */
    void onMessageDelivered(DTMessage msg);

    /**
     * 收到消息被撤回
     */
    void onMessageReCalled(DTMessage msg);

    /**
     * 收到消息状态改变
     *
     * @param input 对方是否在输入
     * @param from  对方的用户名
     */
    void onMessageChanged(boolean input, String from);
    
    
    
    }
    
    记得在不需要的时候移除listener,如在activity的onDestroy()时
    DTClient.getInstance().msgManage().removeMessageListener(msgListener );
    

同步方法

 

  //chatUser为对方用户名或群id,chatType为当前会话的类型(SINGLE,GROUP,CHAT_ROOM)
    DTconversation conversation = DTClient.getInstance().msgManage().getConversation(toChatUsername, chatType);
    
   DTConversation conversation;
   
   //获取与chatUserName的所有聊天记录
   List<DTMessage> allMessage = conversation.getAllMessage();
   
   DTConversation conversation;
   
   //currentPage为当前页数,count为一页获取的消息数量
   List<DTMessage> allMessage = conversation.getMessage(currentPage,count);
   
   DTConversation conversation;

   conversation.getNotReadCount();
   
   DTConversation conversation;
   
   //标记与chatUserName的所有消息为已读(清空会话的未读小红点也可以调用)
   conversation.markAllMessageAsRead();
   
   DTConversation conversation;
    
   conversation.clearChatMsgCountToZero();
   
   DTConversation conversation;
    
   //uuid为消息的uuid
   conversation.markMessageAsRead(uuid);
   
   DTConversation conversation;
    
   //uuid为消息的uuid
   conversation.markMessageAsRead(uuid);
   

同步方法


DTClient.getInstance().msgManage().markAllConversationsAsRead();
   

 DTConversation conversation;

 //获取与chatUserName的消息总数     
 List<DTMessage> allMessage = conversation.getAllMessageCount();

   

异步方法

   //把消息转换成 DTMessage 对象
   DTMessage dtMessage = (DTMessage) object;
   
   //消息监听状态
   dtMessage.setMessageStatusCallBack(sendMessageCallBack);

   //撤回消息
   DTClient.getInstance().msgManage().recallMessage(dtMessage);
   

异步方法


     DTConversation conversation;
   
    //uuid为消息的uuid
    conversation.removeMessage(uuid);

同步方法


   List<DTConversation> allConversations = DTClient.getInstance().msgManage().getAllConversations();
   

同步方法


   //delHistory为是否同时删除所有聊天记录
   List<DTConversation> allConversations = DTClient.getInstance().msgManage().removeAllConversations(delHistory);
   

同步方法


     /**
     * 删除会话
     * @param username   删除的会话用户名或群id
     * @param delHistory 是否同时删除该会话下的聊天记录
     */
   List<DTConversation> allConversations =  DTClient.getInstance().msgManage().deleteConversation(username, delHistory);
   

同步方法


    //username为对方用户名或群id
    DTClient.getInstance().msgManage().removeMessage(username);
   
  • im即时通讯/android/消息.txt
  • 最后更改: 2019/07/08 12:45
  • (外部编辑)