Skip to content

Commit 1aa2d44

Browse files
committed
发红包改为用自定义通知方式
发红包改为用自定义通知方式
1 parent 2e70ffe commit 1aa2d44

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

android/src/main/java/com/netease/im/receiver/CustomNotificationReceiver.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
public class CustomNotificationReceiver extends BroadcastReceiver {
1717

1818
private NotificationManager notificationManager;
19+
1920
@Override
2021
public void onReceive(Context context, Intent intent) {
2122

2223
String action = context.getPackageName() + NimIntent.ACTION_RECEIVE_CUSTOM_NOTIFICATION;
23-
// printIntent(intent);
24+
// printIntent(action, intent);
2425
if (action.equals(intent.getAction())) {
2526

2627
// 从intent中取出自定义通知
@@ -32,14 +33,17 @@ public void onReceive(Context context, Intent intent) {
3233
// }
3334
}
3435
}
36+
3537
public NotificationManager getNotificationManager() {
3638
if (notificationManager == null) {
3739
notificationManager = (NotificationManager) IMApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
3840
}
3941
return notificationManager;
4042
}
41-
void printIntent(Intent intent) {
43+
44+
void printIntent(String action, Intent intent) {
4245
LogUtil.d("NimNetease", "--------------------------------------------");
46+
LogUtil.d("NimNetease", "action" + action);
4347
LogUtil.d("NimNetease", intent.getAction());
4448
Bundle bundle = intent.getExtras();
4549
if (bundle != null) {

android/src/main/java/com/netease/im/session/SessionService.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.ArrayList;
4848
import java.util.Collections;
4949
import java.util.Comparator;
50+
import java.util.Date;
5051
import java.util.HashMap;
5152
import java.util.HashSet;
5253
import java.util.List;
@@ -669,15 +670,18 @@ public void sendDefaultMessage(String type, String digst, String content, OnSend
669670
}
670671

671672
public void sendRedPacketOpenMessage(String sendId, String openId, String hasRedPacket, String serialNo, OnSendMessageListener onSendMessageListener) {
672-
CustomMessageConfig config = new CustomMessageConfig();
673-
config.enableUnreadCount = false;
674-
config.enablePush = false;
675-
RedPacketOpenAttachement attachment = new RedPacketOpenAttachement();
676-
attachment.setParams(sendId, openId, hasRedPacket, serialNo);
677-
IMMessage message = MessageBuilder.createCustomMessage(sessionId, sessionTypeEnum, sendId + ";" + openId, attachment, config);
678-
679-
// message.
680-
sendMessage(message, onSendMessageListener);
673+
// CustomMessageConfig config = new CustomMessageConfig();
674+
// config.enableUnreadCount = false;
675+
// config.enablePush = false;
676+
// RedPacketOpenAttachement attachment = new RedPacketOpenAttachement();
677+
// attachment.setParams(sendId, openId, hasRedPacket, serialNo);
678+
// IMMessage message = MessageBuilder.createCustomMessage(sessionId, sessionTypeEnum, sendId + ";" + openId, attachment, config);
679+
//
680+
//// message.
681+
// sendMessage(message, onSendMessageListener);
682+
long timestamp = new Date().getTime() / 1000;
683+
SessionUtil.sendRedPacketOpenNotification(sessionId, sessionTypeEnum, sendId, openId, hasRedPacket, serialNo, timestamp);
684+
SessionUtil.sendRedPacketOpenLocal(sessionId, sessionTypeEnum, sendId, openId, hasRedPacket, serialNo, timestamp);
681685
}
682686

683687
public void sendRedPacketMessage(String type, String comments, String serialNo, OnSendMessageListener onSendMessageListener) {

android/src/main/java/com/netease/im/session/SessionUtil.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import android.support.v4.app.NotificationCompat;
77

88
import com.netease.im.IMApplication;
9+
import com.netease.im.session.extension.RedPacketOpenAttachement;
910
import com.netease.nimlib.sdk.NIMClient;
11+
import com.netease.nimlib.sdk.msg.MessageBuilder;
1012
import com.netease.nimlib.sdk.msg.MsgService;
13+
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
1114
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
15+
import com.netease.nimlib.sdk.msg.model.CustomMessageConfig;
1216
import com.netease.nimlib.sdk.msg.model.CustomNotification;
1317
import com.netease.nimlib.sdk.msg.model.IMMessage;
1418

@@ -22,6 +26,7 @@
2226
public class SessionUtil {
2327

2428
public final static String CUSTOM_Notification = "1";
29+
public final static String CUSTOM_Notification_redpacket_open = "2";
2530

2631
public static SessionTypeEnum getSessionType(String sessionType) {
2732
SessionTypeEnum sessionTypeE = SessionTypeEnum.None;
@@ -80,7 +85,7 @@ public static void sendAddFriendNotification(String account, String content) {
8085
sendCustomNotification(account, SessionTypeEnum.P2P, CUSTOM_Notification, content);
8186
}
8287

83-
public static void receiver(NotificationManager manager, CustomNotification customNotification){
88+
public static void receiver(NotificationManager manager, CustomNotification customNotification) {
8489
Map<String, Object> map = customNotification.getPushPayload();
8590
if (map != null && map.containsKey("type")) {
8691
String type = (String) map.get("type");
@@ -93,10 +98,34 @@ public static void receiver(NotificationManager manager, CustomNotification cust
9398
IMApplication.getContext(), 0, new Intent(IMApplication.getContext(), IMApplication.getMainActivityClass()), 0);
9499
builder.setContentIntent(contentIntent);
95100
builder.setSmallIcon(IMApplication.getNotify_msg_drawable_id());
96-
manager.notify((int) System.currentTimeMillis(),builder.build());
101+
manager.notify((int) System.currentTimeMillis(), builder.build());
102+
} else if (SessionUtil.CUSTOM_Notification_redpacket_open.equals(type)) {
103+
Map<String, Object> data = (Map<String, Object>) map.get("data");
104+
105+
Map<String, Object> dict = (Map<String, Object>) data.get("dict");
106+
String sendId = (String) dict.get("sendId");
107+
String openId = (String) dict.get("openId");
108+
String hasRedPacket = (String) dict.get("hasRedPacket");
109+
String serialNo = (String) dict.get("serialNo");
110+
111+
String timestamp = (String) data.get("timestamp");
112+
long t = 0L;
113+
try {
114+
t = Long.parseLong(timestamp);
115+
} catch (NumberFormatException e) {
116+
t = System.currentTimeMillis()/1000;
117+
e.printStackTrace();
118+
}
119+
// LogUtil.i("timestamp","timestamp:"+timestamp);
120+
// LogUtil.i("timestamp","t:"+t);
121+
// LogUtil.i("timestamp",""+data);
122+
String sessionId = (String) data.get("sessionId");
123+
String sessionType = (String) data.get("sessionType");
124+
sendRedPacketOpenLocal(sessionId, getSessionType(sessionType), sendId, openId, hasRedPacket, serialNo, t);
97125
}
98126
}
99127
}
128+
100129
/**
101130
* @param account
102131
* @param sessionType
@@ -119,4 +148,48 @@ public static void sendCustomNotification(String account, SessionTypeEnum sessio
119148

120149
NIMClient.getService(MsgService.class).sendCustomNotification(notification);
121150
}
151+
152+
public static void sendRedPacketOpenLocal(String sessionId, SessionTypeEnum sessionType,
153+
String sendId, String openId, String hasRedPacket, String serialNo, long timestamp) {
154+
155+
CustomMessageConfig config = new CustomMessageConfig();
156+
config.enableUnreadCount = false;
157+
config.enablePush = false;
158+
RedPacketOpenAttachement attachment = new RedPacketOpenAttachement();
159+
attachment.setParams(sendId, openId, hasRedPacket, serialNo);
160+
IMMessage message = MessageBuilder.createCustomMessage(sessionId, sessionType, attachment.getTipMsg(true), attachment, config);
161+
message.setStatus(MsgStatusEnum.success);
162+
163+
message.setConfig(config);
164+
NIMClient.getService(MsgService.class).saveMessageToLocalEx(message, true, timestamp * 1000);
165+
}
166+
167+
public static void sendRedPacketOpenNotification(String sessionId, SessionTypeEnum sessionType,
168+
String sendId, String openId, String hasRedPacket, String serialNo, long timestamp) {
169+
170+
Map<String, Object> data = new HashMap<>();
171+
Map<String, String> dict = new HashMap<>();
172+
dict.put("sendId", sendId);
173+
dict.put("openId", openId);
174+
dict.put("hasRedPacket", hasRedPacket);
175+
dict.put("serialNo", serialNo);
176+
177+
data.put("dict", dict);
178+
data.put("timestamp", Long.toString(timestamp));
179+
data.put("sessionId", sessionId);
180+
data.put("sessionType", Integer.toString(sessionType.getValue()));
181+
182+
CustomNotification notification = new CustomNotification();
183+
notification.setSessionId(sessionId);
184+
notification.setSessionType(sessionType);
185+
186+
notification.setSendToOnlineUserOnly(false);
187+
188+
Map<String, Object> pushPayload = new HashMap<>();
189+
pushPayload.put("type", CUSTOM_Notification_redpacket_open);
190+
pushPayload.put("data", data);
191+
notification.setPushPayload(pushPayload);
192+
193+
NIMClient.getService(MsgService.class).sendCustomNotification(notification);
194+
}
122195
}

0 commit comments

Comments
 (0)