欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

网易云信云叮当红包接入流程分享 Android网易云信红包SDK 

程序员文章站 2022-06-12 13:23:28
...

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

 


一、文档说明

基于网易云信的IM,接入云叮当红包SDK实现App快速集成发单聊红包、发群聊红包、拆红包并查看交易记录等功能.

在阅读下面内容之前我们假设你有一定的android开发能力并且你的项目中已经接入过网易云信IM和云叮当红包.或者完全当做教程。

二、集成流程详解

  1. 在app的application类中初始化网易云信,UIkit,云叮当红包

    public class MyApplication extends Application {
    
        public void onCreate() {
    
            super.onCreate();
    
            // ... your codes
    
            NIMClient.init(this, null, null);
    
            // ... your codes
    
            if (inMainProcess()) {
            // 初始化UIKit模块
                initUIKit();
                NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser());
                NimUIKit.registerMsgItemViewHolder(fghGuessAttachment.class,fghSessionHelper.class);
            }
    
            //初始化云叮当红包模块
            RedPacket.initReaPacket(this, new AppSecretCallBack(), new UseOrderInfoLinster());
    
            //打开云叮当红包日志
            RedPacketLog.DEBUG = true;
        }
    }
    
  2. 用户登陆时载入用户信息

    RedPacket.initReaPacket(String account, String nickName, String serialNumber);
    
  3. 新建一个PacketAction对象

    public class PacketAction extends BaseAction {
    
        public PacketAction() {
            //图标和名称对应的ID
            super(R.drawable.redpacket, R.string.input_panel_redpacket);
        }
    
        @Override
        public void onClick() {
            //点击事件,调用发红包页面(当然,你也可以在其他地方调用起发红包界面)
             RedPacket.sendPacket(Context context, 1, String receivedUID, String );
        }
    }
    
  4. MessageFragment类里面添加PacketAction

    // 操作面板集合
    protected List<BaseAction> getActionList() {
        List<BaseAction> actions = new ArrayList<>();
        //......
        actions.add(new PacketAction());
    
        if (customization != null &&  customization.actions != null) {
            actions.addAll(customization.actions);
        }
        return actions;
    }
    

    这时,你就能在操作面板中看到发红包的入口了

    接下来,我们要自定义网易云信的消息

  5. 定义一个红包消息的类型

    public interface CustomAttachmentType {
        // 多端统一
        int Guess = 1;
        int SnapChat = 2;
        int Sticker = 3;
        int RTS = 4;
        int SHARE  = 5;//自定义
    }
    
  6. 定义一个红包消息的基类

    public abstract class CustomAttachment implements MsgAttachment {
    
        protected int type;
    
        CustomAttachment(int type) {
            this.type = type;
        }
    
        public void fromJson(JSONObject data) {
            if (data != null) {
    
                parseData(data);
    
            }
        }
    
        @Override
        public String toJson(boolean send) {
            return CustomAttachParser.packData(type, packData());
        }
    
        public int getType() {
            return type;
        }
    
        protected abstract void parseData(JSONObject data);
    
        protected abstract JSONObject packData();
    }
    
  7. 继承这个基类,解析消息

    public class PacketAttachment extends CustomAttachment {
    
    
        private String packetID;
        private String packetType;
    
    
        protected int type;
    
    
        public PacketAttachment() {
            super(CustomAttachmentType.SHARE);
    
        }
    
        public PacketAttachment(String str) {
            this();
        }
    
        @Override
        protected void parseData(JSONObject data) {
            packetID = data.getString("packetID");
            packetType = data.getString("packetType");
        }
    
        @Override
        public JSONObject packData() {
            JSONObject data = new JSONObject();
            data.put("packetID",packetID);
            data.put("packetType",packetType);
            return data;
        }
    
        public String getPacketType() {
            return packetType;
        }
    
        public void setPacketType(String packetType) {
            this.packetType = packetType;
        }
    
        public String getPacketID() {
            return packetID;
        }
    
        public void setPacketID(String packetID) {
            this.packetID = packetID;
        }
    
    }
    
  8. 实现红包消息的解析器

    // 在Application初始化中注册自定义消息附件解析器 
    NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser());
    
    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 = JSON.parseObject(json);
                int type = object.getInteger(KEY_TYPE);
                JSONObject data = object.getJSONObject(KEY_DATA);
                switch (type) {
                    case CustomAttachmentType.Guess:
                        attachment = new PacketAttachment();
                        break;
                    default:
                        attachment = new DefaultCustomAttachment();
                        break;
                }
    
                if (attachment != null) {
                    attachment.fromJson(data);
                }
            } catch (Exception e) {
            }
    
            return attachment;
        }
    
        public static String packData(int type, JSONObject data) {
            JSONObject object = new JSONObject();
            object.put(KEY_TYPE, type);
            if (data != null) {
                object.put(KEY_DATA, data);
            }
    
            return object.toJSONString();
        }
    } 
    
  9. 将自定义消息展示UI上

    public class PacketSessionHelper extends MsgViewHolderBase{
    
        private PacketAttachment attachment;
    
        @Override
        protected int getContentResId() {
            return R.layout.abcdefg;
        }
    
        @Override
        protected void inflateContentView() {
    
            //你的代码
    
        }
    
        @Override
        protected void bindContentView() {
    
            //你的代码
        }
    
        //若是要自己修改气泡背景
        // 当是发送出去的消息时,内容区域背景的drawable id
        @Override
        protected int rightBackground() {
            return com.netease.nim.uikit.R.drawable.nim_message_item_right_selector;
        }
    
    }
    
  10. 发送红包消息

    activity中重写onActivityResult方法

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case 1:
                    switch (resultCode) {
                        case 1:
                           PacketAttachment attachment = new PacketAttachment();
                           IMMessage message;
                           if (getContainer() != null && getContainer().sessionType == SessionTypeEnum.ChatRoom) {
                                message = ChatRoomMessageBuilder.createChatRoomCustomMessage(getAccount(), attachment);
                           } else {
                                message = MessageBuilder.createCustomMessage(getAccount(), getSessionType(), attachment);
                           }
                           sendMessage(message);
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    
  11. 在app的application类中注册消息解析器和服务

    NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser());
    NimUIKit.registerMsgItemViewHolder(PacketAttachment.class,PacketSessionHelper.class);
    
  12. PacketSessionHelper中重写onItemClick方法,实现开红包功能

    // 内容区域点击事件响应处理。
    @Override
    protected void onItemClick() {
        RedPacket.openPacket(FragmentActivity activity, 
                          String platRpNo,
                          @Nullable String outReceiveNo,
                          @Nullable String outGroupId,
                          new RedPacketCallBack callBack()){
            //代码省略
        };
    }

 

欢迎交流~

邮箱:dingdangcloug@huanwu.tech 

官网:http://yundingdang.com/official/index.html#/