66import android .support .v4 .app .NotificationCompat ;
77
88import com .netease .im .IMApplication ;
9+ import com .netease .im .session .extension .RedPacketOpenAttachement ;
910import com .netease .nimlib .sdk .NIMClient ;
11+ import com .netease .nimlib .sdk .msg .MessageBuilder ;
1012import com .netease .nimlib .sdk .msg .MsgService ;
13+ import com .netease .nimlib .sdk .msg .constant .MsgStatusEnum ;
1114import com .netease .nimlib .sdk .msg .constant .SessionTypeEnum ;
15+ import com .netease .nimlib .sdk .msg .model .CustomMessageConfig ;
1216import com .netease .nimlib .sdk .msg .model .CustomNotification ;
1317import com .netease .nimlib .sdk .msg .model .IMMessage ;
1418
2226public 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