NetEase Yunxin Cloud Dingdang Red Packet Access Process Sharing

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

 


1. Documentation

Based on NetEase Yunxin's IM, access the Yundingdang Red Packet SDK to quickly integrate the functions of the App, such as sending orders and chatting red packets, sending group chatting red packets, opening red packets and viewing transaction records.

Before reading the following content, we assume that you have certain android development capabilities and that your project has been connected to NetEase Yunxin IM and Yundingdang Red Packet. Or it is completely a tutorial.

2. Detailed explanation of the integration process

  1. applicationInitialize NetEase Yunxin, UIkit, and Cloud Dingdang red envelopes in the app's class

    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. Load user information when user logs in

    RedPacket.initReaPacket(String account, String nickName, String serialNumber);
    
  3. create a new PacketActionobject

    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. add in MessageFragmentclassPacketAction

    // 操作面板集合
    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;
    }
    

    At this time, you can see the entrance of the red envelope in the operation panel

    Next, we want to customize the message of NetEase Yunxin

  5. Define the type of a red envelope message

    public interface CustomAttachmentType {
        // 多端统一
        int Guess = 1;
        int SnapChat = 2;
        int Sticker = 3;
        int RTS = 4;
        int SHARE  = 5;//自定义
    }
    
  6. Define a base class for red envelope messages

    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. Inherit this base class to parse the message

    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. A parser that implements red envelope messages

    // 在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. Display the custom message on the 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. Send a red envelope message

    activityOverride onActivityResultmethod in

     @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. applicationRegister message parser and service in app's class

    NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser());
    NimUIKit.registerMsgItemViewHolder(PacketAttachment.class,PacketSessionHelper.class);
    
  12. PacketSessionHelperRewrite the onItemClickmethod in , to realize the function of opening red envelopes

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

 

Welcome to communicate~

Email: [email protected] 

Official website: http://yundingdang.com/official/index.html#/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326201578&siteId=291194637