美文网首页
网易云信

网易云信

作者: yangwenaho | 来源:发表于2019-01-23 16:48 被阅读0次

    项目用到的网易云信有单聊和发送货源链接的功能:


    D)LA}OWGQC`%F%F0B5MO)Q3.png

    第一步:在application那边初始化配置:

    @Override
        public void onCreate() {
            super.onCreate();
            AbstractProxyObserver.setLoginClass(LoginActivity.class);
            NIMClient.init(this,loginfo(),new SDKOptions());
            if (NIMUtil.isMainProcess(this)){
                //初始化UIKIT模块
                initUIKIT();
            }
        }
    
        private void initUIKIT() {
            //初始化
            NimUIKit.init(this,buildUIKitOptions());
            //IM会话窗口定制初始化
            SessionHelper.init();
            // 设置地理位置提供者。如果需要发送地理位置消息,该参数必须提供。如果不需要,可以忽略。
            NimUIKit.setLocationProvider(new NimDemoLocationProvider());
        }
        private UIKitOptions buildUIKitOptions() {
            UIKitOptions options = new UIKitOptions();
            // 设置app图片/音频/日志等缓存目录
            options.appCacheDir = NimSDKOptionConfig.getAppCacheDir(this) + "/app";
            return options;
        }
        private LoginInfo loginfo() {
            String account  = SpUtil.getString(ConstUtils.IM_ACCID);
            String token  = SpUtil.getString(ConstUtils.IM_TOKEN);
            if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token)){
                DemoCache.setAccount(account.toLowerCase());
                return new LoginInfo(account,token);
            }
            return null;
        }
    

    第二步:自定义类继承P2PMessageActivity:

    public class CommodityActivity extends P2PMessageActivity{
        public  static void start(Context context, String contactId, SessionCustomization customization,Bundle bundle){
            Intent intent = new Intent();
            intent.putExtra(Extras.EXTRA_ACCOUNT, contactId);
            intent.putExtra(Extras.EXTRA_CUSTOMIZATION, customization);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            intent.setClass(context, CommodityActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(intent);
    
        }
        
    }
    

    第三步:跳转到聊天界面:

    Bundle bundle = new Bundle();
                    //在这里构造一个自定义消息
                    bundle.putSerializable("data",detailsBean);//实体数据
                    CommodityActivity.start(getActivity(),detailsBean.getUser().getAccid(), SessionHelper.getMyP2pCustomization(),bundle);
    

    这里的数据是聊天界面需要的数据。
    第四步:在MessageFragment 类中自定义自己的头部局,然后拿到实体做进一步操作。
    获取实体数据:

     data = (DetailsBean) getArguments().getSerializable("data");
    

    拿到数据后进行相关的操作。
    第五步:在CustomAttachmentType自定义类型:

    int Supply= 7;
    

    第六步:进行相关的注册

    public class CustomAttachParser implements MsgAttachmentParser {
    
        private static final String KEY_TYPE = "type";
        private static final String KEY_DATA = "data";
    
        @Override
        public MsgAttachment parse(String json) {
            CustomAttachment attachment = null;
            try {
    
                JSONObject object = new JSONObject(json);
                int type = object.getInt(KEY_TYPE);
                JSONObject data = object.getJSONObject(KEY_DATA);
                switch (type) {
    
                    case CustomAttachmentType.SnapChat:
                        return new SnapChatAttachment(data);
                    case CustomAttachmentType.Sticker:
                        attachment = new StickerAttachment();
                        break;
                    case CustomAttachmentType.RTS:
                        attachment = new RTSAttachment();
                        break;        
                    case CustomAttachmentType.Supply:
                        attachment = new CommodityMoneyAttachment();
                        break;
                   
                }
    
                if (attachment != null) {
                    attachment.fromJson(data);
                }
            } catch (Exception e) {
    
            }
    
            return attachment;
        }
    
        
        public static String packData(int type, JSONObject data) {
            JSONObject object = new JSONObject();
            try {
                object.put(KEY_TYPE, type);
                if (data != null) {
                    object.put(KEY_DATA, data);
                }
            } catch (JSONException e) {
            }
            return object.toString();
        }
    
    
    NimUIKit.registerMsgItemViewHolder(MoneyAttachment.class, MsgViewHolderMoney.class);
    

    第七步:解析数据格式相关的类

    public class SupplyAttachment extends CustomAttachment implements Serializable{
    
       private static final String STARTCITY = "startcity";
       private static final String ENDCITY = "endcity";
       private static final String TYPES = "types";
       private static final String ID = "id";
       private static final String ORDER = "order";
       private static final String IDENTIFICATION_MODEL = "Identification_Model";
    
       private String startcity;
       private String endcity;
       private String types;
       private String id;
       private String order;
       private String Identification_Model;
    
    
       public SupplyAttachment() {
           super(CustomAttachmentType.Supply);
    
       }
    
       @Override
       public String toJson(boolean send) {
           return CustomAttachParser.packData(CustomAttachmentType.Supply,packData());
       }
    
       @Override
       protected void parseData(JSONObject data) {
           startcity = data.optString(STARTCITY);
           endcity = data.optString(ENDCITY);
           types = data.optString(TYPES);
           id = data.optString(ID);
           order = data.optString(ORDER);
           Identification_Model = data.optString(IDENTIFICATION_MODEL);
    
       }
    
    
    
       //数据打包
       @Override
       protected JSONObject packData() {
           JSONObject data = new JSONObject();
           try {
               data.put(STARTCITY,startcity);
               data.put(ENDCITY,endcity);
               data.put(TYPES,types);
               data.put(ID,id);
               data.put(ORDER,order);
               data.put(IDENTIFICATION_MODEL,Identification_Model);
           } catch (JSONException e) {
               e.printStackTrace();
           }
    
           return data;
       }
    
    
       public String getIdentification_Model() {
           return Identification_Model;
       }
    
       public void setIdentification_Model(String identification_Model) {
           Identification_Model = identification_Model;
       }
    
       public String getStartcity() {
           return startcity;
       }
    
       public void setStartcity(String startcity) {
           this.startcity = startcity;
       }
    
       public String getEndcity() {
           return endcity;
       }
    
       public void setEndcity(String endcity) {
           this.endcity = endcity;
       }
    
    
       public String getTypes() {
           return types;
       }
    
       public void setTypes(String types) {
           this.types = types;
       }
    
       public String getId() {
           return id;
       }
    
       public void setId(String id) {
           this.id = id;
       }
    
        public String getOrder() {
           return order;
       }
    
       public void setOrder(String order) {
           this.order = order;
       }
    
       @Override
       public String toString() {
           return "SupplyAttachment{" +
                   "startcity='" + startcity + '\'' +
                   ", endcity='" + endcity + '\'' +
                   ", types='" + types + '\'' +
                   ", id='" + id + '\'' +
                   ", order='" + order + '\'' +
                   '}';
       }
    }
    
    

    第八步:在MessageFragment界面发送数据:

    @Override
       public void onClick(View v) {
           
           SupplyAttachment attachment = new SupplyAttachment();
           attachment.setStartcity(data.getStart_city_name());
           attachment.setEndcity(data.getEnd_city_name());
           attachment.setId(data.getId());
           attachment.setTypes(data.getFreight_detail()+"/"+data.getCar_type_and_length());
           if (data.getOrder_number() != null){
               attachment.setOrder("订单号:"+ data.getOrder_number());
           }else {
               attachment.setOrder("");
           }
    
           Gson gson = new Gson();
           String jsonStr = gson.toJson(data);
           attachment.setIdentification_Model(jsonStr);
    
           IMMessage  message = MessageBuilder.createCustomMessage(sessionId, SessionTypeEnum.P2P,attachment);
           sendMessage(message);
       }
    

    sessionId是对方的账号。

    最后需要云信demo的UIKIT项目包。

    相关文章

      网友评论

          本文标题:网易云信

          本文链接:https://www.haomeiwen.com/subject/kcfsjqtx.html