奖励消息

This commit is contained in:
18401019693 2022-09-06 16:36:34 +08:00
parent 4560c00810
commit ec8e258e45
8 changed files with 164 additions and 7 deletions

View File

@ -177,7 +177,7 @@ public class AppContext extends CommonAppContext {
RongIMClient.registerMessageType(myMessages);
// 注册自定义消息模板
RongConfigCenter.conversationConfig().addMessageProvider(new InstructorSendRewardProvider());
RongConfigCenter.conversationConfig().addMessageProvider(new InstructorSendRewardProvider(getApplicationContext()));
RongConfigCenter.conversationConfig().addMessageProvider(new RecommendLiveRoomProvider(getApplicationContext()));
RongcloudIMManager.addRongcloudIMOnReceiveMessageListener(new RongIMClient.OnReceiveMessageWrapperListener() {

View File

@ -0,0 +1,34 @@
package com.yunbao.common.bean;
import com.google.gson.annotations.SerializedName;
public class InstructorSendRewardModel extends BaseModel {
/**
* img : https://downs.yaoulive.com/Gift_Pictures%402x.png
* admin_nickname : null
*/
@SerializedName("img")
private String img = "";
@SerializedName("admin_nickname")
private String adminNickname = "";
public String getImg() {
return img;
}
public InstructorSendRewardModel setImg(String img) {
this.img = img;
return this;
}
public String getAdminNickname() {
return adminNickname;
}
public InstructorSendRewardModel setAdminNickname(String adminNickname) {
this.adminNickname = adminNickname;
return this;
}
}

View File

@ -2,18 +2,86 @@ package com.yunbao.common.manager.imrongcloud;
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
@SuppressLint("ParcelCreator")
@MessageTag(value = "InstructorSendReward", flag = MessageTag.ISCOUNTED)
public class InstructorSendReward extends MessageContent {
public class InstructorSendReward extends MessageContent implements Parcelable {
public InstructorSendReward(byte[] data) {
if (data == null) {
return;
}
String jsonStr = null;
try {
jsonStr = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
if (jsonStr == null) {
return;
}
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// 消息携带用户信息时, 自定义消息需添加下面代码
if (jsonObj.has("user")) {
setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));
}
// 用于群组聊天, 消息携带 @ 人信息时, 自定义消息需添加下面代码
if (jsonObj.has("mentionedInfo")) {
setMentionedInfo(parseJsonToMentionInfo(jsonObj.getJSONObject("mentionedInfo")));
}
// 将所有自定义变量从收到的 json 解析并赋值
if (jsonObj.has("content")) {
content = jsonObj.optString("content");
}
} catch (JSONException e) {
}
}
// 自定义消息变量可以有多个
private String content;
public InstructorSendReward setContent(String content) {
this.content = content;
return this;
}
public String getContent() {
return content;
}
@Override
public byte[] encode() {
return new byte[0];
JSONObject jsonObj = new JSONObject();
try {
// 消息携带用户信息时, 自定义消息需添加下面代码
if (getJSONUserInfo() != null) {
jsonObj.putOpt("user", getJSONUserInfo());
}
// 用于群组聊天, 消息携带 @ 人信息时, 自定义消息需添加下面代码
if (getJsonMentionInfo() != null) {
jsonObj.putOpt("mentionedInfo", getJsonMentionInfo());
}
// 将所有自定义消息的内容都序列化至 json 对象中
jsonObj.put("content", this.content);
} catch (JSONException e) {
}
try {
return jsonObj.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
}
return null;
}
@Override
@ -23,6 +91,24 @@ public class InstructorSendReward extends MessageContent {
@Override
public void writeToParcel(Parcel dest, int flags) {
ParcelUtils.writeToParcel(dest, getExtra());
ParcelUtils.writeToParcel(dest, content);
}
public static final Creator<InstructorSendReward> CREATOR = new Creator<InstructorSendReward>() {
@Override
public InstructorSendReward createFromParcel(Parcel source) {
return new InstructorSendReward(source);
}
@Override
public InstructorSendReward[] newArray(int size) {
return new InstructorSendReward[size];
}
};
public InstructorSendReward(Parcel in) {
setExtra(ParcelUtils.readFromParcel(in));
setContent(ParcelUtils.readFromParcel(in));
}
}

View File

@ -1,9 +1,16 @@
package com.yunbao.common.manager.imrongcloud;
import android.content.Context;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.ViewGroup;
import com.google.gson.Gson;
import com.yunbao.common.R;
import com.yunbao.common.bean.InstructorSendRewardModel;
import java.util.List;
import io.rong.imkit.conversation.messgelist.provider.BaseMessageItemProvider;
@ -16,6 +23,13 @@ import io.rong.imlib.model.MessageContent;
* 自定义消息模板
*/
public class InstructorSendRewardProvider extends BaseMessageItemProvider<InstructorSendReward> {
private Context mContext;
private String politicalInstructor = "";
public InstructorSendRewardProvider(Context context) {
this.mContext = context;
}
/**
* 创建 ViewHolder
*
@ -25,7 +39,7 @@ public class InstructorSendRewardProvider extends BaseMessageItemProvider<Instru
*/
@Override
protected ViewHolder onCreateMessageContentViewHolder(ViewGroup viewGroup, int viewType) {
return null;
return ViewHolder.createViewHolder(mContext, viewGroup, R.layout.view_instructor_send_reward);
}
/**
@ -41,7 +55,11 @@ public class InstructorSendRewardProvider extends BaseMessageItemProvider<Instru
*/
@Override
protected void bindMessageContentViewHolder(ViewHolder holder, ViewHolder parentHolder, InstructorSendReward instructorSendReward, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
if (uiMessage.getMessage().getContent() instanceof InstructorSendReward) {
InstructorSendReward recommendLiveRoom = (InstructorSendReward) uiMessage.getMessage().getContent();
InstructorSendRewardModel model = new Gson().fromJson(recommendLiveRoom.getContent(), InstructorSendRewardModel.class);
holder.setImageUri(R.id.live_bg, Uri.parse(model.getImg()));
}
}
/**
@ -79,6 +97,11 @@ public class InstructorSendRewardProvider extends BaseMessageItemProvider<Instru
*/
@Override
public Spannable getSummarySpannable(Context context, InstructorSendReward instructorSendReward) {
return null;
if (instructorSendReward != null && !TextUtils.isEmpty(instructorSendReward.getContent())) {
InstructorSendRewardModel model = new Gson().fromJson(instructorSendReward.getContent(), InstructorSendRewardModel.class);
politicalInstructor = model.getAdminNickname();
politicalInstructor = TextUtils.isEmpty(politicalInstructor) ? "" : politicalInstructor;
}
return new SpannableString(politicalInstructor + mContext.getString(R.string.bonus_message));
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="238dp"
android:layout_height="183dp">
<ImageView
android:id="@+id/live_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</RelativeLayout>

View File

@ -871,4 +871,5 @@ Limited ride And limited avatar frame</string>
<string name="shield_mount_effect">Shield car effect</string>
<string name="current_live_room">You are in the current live room</string>
<string name="live_room_air_ticket">has recommended an anchor to you!</string>
<string name="bonus_message">has sent you a reward, come and get it!</string>
</resources>

View File

@ -888,5 +888,5 @@
<string name="popular_tickets">人氣票</string>
<string name="system_notice">系統通知</string>
<string name="online_service">在線客服</string>
<string name="bonus_message">向你發送了一個獎勵,快來領取吧!</string>
</resources>

View File

@ -74,6 +74,7 @@ public class RecommendLiveRoomProvider extends BaseMessageItemProvider<Recommend
if (recommendLiveRoom != null && !TextUtils.isEmpty(recommendLiveRoom.getContent())) {
RecommendLiveRoomModel model = new Gson().fromJson(recommendLiveRoom.getContent(), RecommendLiveRoomModel.class);
politicalInstructor = model.getAdminNickname();
politicalInstructor = TextUtils.isEmpty(politicalInstructor) ? "" : politicalInstructor;
}
return new SpannableString(politicalInstructor + mContext.getString(R.string.live_room_air_ticket));
}