新开商户可免费发送累计5000元总额的红包,超过5000元后,红包功能自动关闭,直到商户付费购买增值服务“红包功能”后,该功能重新开启,购买增值服务请咨询环信商务经理。
1.redpacketlibrary,在环信SDK的基础上提供了收发红包和零钱的功能。
2.环信官方版Demo已默认集成红包功能,可以直接下载试用。
dependencies {
compile project(':redpacketlibrary')
compile project(':EaseUI')
compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-multidex.jar')
}
include ':EaseUI', ':redpacketlibrary'
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19"
tools:overrideLibrary="com.easemob.redpacketui"
/>
<!--红包相关界面start-->
<activity
android:name="com.easemob.redpacketui.ui.activity.RPRedPacketActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustPan|stateVisible"
/>
<activity
android:name="com.easemob.redpacketui.ui.activity.RPDetailActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustPan"
/>
<activity
android:name="com.easemob.redpacketui.ui.activity.RPRecordActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustPan"
/>
<activity
android:name="com.easemob.redpacketui.ui.activity.RPWebViewActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustResize|stateHidden"
/>
<activity
android:name="com.easemob.redpacketui.ui.activity.RPChangeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustResize|stateHidden"
/>
<activity
android:name="com.easemob.redpacketui.ui.activity.RPBankCardActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustPan|stateHidden"
/>
<activity
android:name="com.easemob.redpacketui.ui.activity.RPGroupMemberActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide"
android:windowSoftInputMode="adjustPan|stateHidden"
/>
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden"
/>
<!--红包相关界面end-->
import com.easemob.redpacketsdk.RedPacket;
@Override
public void onCreate() {
super.onCreate();
RedPacket.getInstance().initContext(applicationContext);
//打开Log开关 正式发布时请关闭
RedPacket.getInstance().setDebugMode(true);
}
private static final int MESSAGE_TYPE_RECV_RED_PACKET = 5;
private static final int MESSAGE_TYPE_SEND_RED_PACKET = 6;
private static final int MESSAGE_TYPE_SEND_RED_PACKET_ACK = 7;
private static final int MESSAGE_TYPE_RECV_RED_PACKET_ACK = 8;
private static final int ITEM_RED_PACKET = 16;
private static final int REQUEST_CODE_SEND_RED_PACKET = 15;
@Override
protected void registerExtendMenuItem() {
//demo这里不覆盖基类已经注册的item,item点击listener沿用基类的
super.registerExtendMenuItem();
//聊天室暂时不支持红包功能
if (chatType != Constant.CHATTYPE_CHATROOM) {
inputMenu.registerExtendMenuItem(R.string.attach_red_packet, R.drawable.em_chat_red_packet_selector, ITEM_RED_PACKET, extendMenuItemClickListener);
}
}
if (type == EMMessage.Type.TXT.ordinal()) {
if(message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false) ||
message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)
|| message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, false)){
setContentView(R.layout.em_context_menu_for_location);
}
}
if (message.direct == EMMessage.Direct.RECEIVE || message.getChatType() == EMMessage.ChatType.ChatRoom
//red packet code : 屏蔽红包消息的撤回功能
|| message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, false)) {
//end of red packet code
View view = findViewById(R.id.text_revoke);
if(view != null){
view.setVisibility(View.GONE);
}
}
@Override
public boolean onExtendMenuItemClick(int itemId, View view) {
switch (itemId) {
...
case ITEM_RED_PACKET:
RedPacketUtils.startRedPacketActivityForResult(this, chatType, toChatUsername, REQUEST_CODE_SEND_RED_PACKET);
break;
default:
break;
}
//不覆盖已有的点击事件
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
if(resultCode == Activity.RESULT_OK){
switch (requestCode) {
...
case REQUEST_CODE_SEND_RED_PACKET:
if (data != null){
sendMessage(RedPacketUtils.createRPMessage(getActivity(), data, toChatUsername));
}
break;
default:
break;
}
}
}
@Override
public boolean onMessageBubbleClick(EMMessage message) {
//消息框点击事件,demo这里不做覆盖,如需覆盖,return true
if (message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, false)){
RedPacketUtils.openRedPacket(getActivity(), chatType, message, toChatUsername, messageList);
return true;
}
return false;
}
@Override
public void onEvent(EMNotifierEvent event) {
switch (event.getEvent()) {
case EventNewCMDMessage:
EMMessage cmdMessage = (EMMessage) event.getData();
CmdMessageBody cmdMsgBody = (CmdMessageBody) cmdMessage.getBody();
final String action = cmdMsgBody.action;//获取自定义action
if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)) {
RedPacketUtil.receiveRedPacketAckMessage(cmdMessage);
messageList.refresh();
}
break;
}
super.onEvent(event);
}
@Override
public void onEvent(EMNotifierEvent event) {
switch (event.getEvent()) {
...
case EventNewCMDMessage:
EMMessage cmdMessage = (EMMessage) event.getData();
//获取消息body
CmdMessageBody cmdMsgBody = (CmdMessageBody) cmdMessage.getBody();
final String action = cmdMsgBody.action;//获取自定义action
...
if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)) {
RedPacketUtil.receiveRedPacketAckMessage(cmdMessage);
}
refreshUIWithMessage();
break;
default:
break;
}
}
protected void registerEventListener() {
eventListener = new EMEventListener() {
private BroadcastReceiver broadCastReceiver = null;
@Override
public void onEvent(EMNotifierEvent event) {
switch (event.getEvent()) {
...
case EventNewCMDMessage:
//获取消息body
CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody();
final String action = cmdMsgBody.action;//获取自定义action
if(!easeUI.hasForegroundActivies()){
if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)){
RedPacketUtil.receiveRedPacketAckMessage(message);
broadcastManager.sendBroadcast(new Intent(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION));
}
}
break;
...
}
}
};
EMChatManager.getInstance().registerEventListener(eventListener);
}
private void registerBroadcastReceiver() {
broadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Constant.ACTION_CONTACT_CHANAGED);
intentFilter.addAction(Constant.ACTION_GROUP_CHANAGED);
intentFilter.addAction(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)){
if (conversationListFragment != null){
conversationListFragment.refresh();
}
}
}
};
broadcastManager.registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void setUpView() {
...
conversationListView.setConversationListHelper(new EaseConversationListHelper() {
@Override
public String onSetItemSecondaryText(EMMessage lastMessage) {
if (lastMessage.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_ACK_MESSAGE, false)) {
String sendNick = lastMessage.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_SENDER_NAME, "");
String receiveNick = lastMessage.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_RECEIVER_NAME, "");
String msg;
if (lastMessage.direct == EMMessage.Direct.RECEIVE) {
msg = String.format(getResources().getString(R.string.money_msg_someone_take_money), receiveNick);
} else {
if (sendNick.equals(receiveNick)) {
msg = getResources().getString(R.string.money_msg_take_money);
} else {
msg = String.format(getResources().getString(R.string.money_msg_take_someone_money), sendNick);
}
}
return msg;
}
return null;
}
});
super.setUpView();
}