NIMSDK-AOS 10.4.0-beta
载入中...
搜索中...
未找到
MessageBuilder.java
浏览该文件的文档.
1package com.netease.nimlib.sdk.msg;
2
3import android.content.Context;
4import android.net.Uri;
5import android.text.TextUtils;
6
7import com.netease.nimlib.BuildConfig;
8import com.netease.nimlib.NimNosSceneKeyConstant;
9import com.netease.nimlib.SDKCache;
10import com.netease.nimlib.SDKCacheUI;
11import com.netease.nimlib.biz.constant.IAuthService;
12import com.netease.nimlib.migration.MessageConvert;
13import com.netease.nimlib.notifier.support26.annotation.NonNull;
14import com.netease.nimlib.sdk.msg.attachment.AudioAttachment;
15import com.netease.nimlib.sdk.msg.attachment.FileAttachment;
16import com.netease.nimlib.sdk.msg.attachment.ImageAttachment;
17import com.netease.nimlib.sdk.msg.attachment.LocationAttachment;
18import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
19import com.netease.nimlib.sdk.msg.attachment.VideoAttachment;
20import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
21import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum;
22import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
23import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
24import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
25import com.netease.nimlib.sdk.msg.model.CustomMessageConfig;
26import com.netease.nimlib.sdk.msg.model.IMMessage;
27import com.netease.nimlib.sdk.msg.model.MessageRobotInfo;
28import com.netease.nimlib.sdk.robot.model.RobotAttachment;
29import com.netease.nimlib.sdk.robot.model.RobotMsgType;
30import com.netease.nimlib.sdk.util.UriUtils;
31import com.netease.nimlib.session.IMMessageImpl;
32import com.netease.nimlib.util.BitmapDecoder;
33import com.netease.nimlib.util.StringUtil;
34import com.netease.nimlib.util.TimeUtil;
35
36import org.json.JSONException;
37import org.json.JSONObject;
38
39import java.io.File;
40import java.util.ArrayList;
41import java.util.List;
42
43/**
44 * 云信 IM 消息构造器,提供构建各类型消息的接口。
45 */
46public class MessageBuilder {
47
48 /**
49 * 创建一条文本消息。
50 *
51 * @par 使用前提:
52 * 已登录 IM。
53 * @par 参数说明:
54 * <table>
55 * <tr>
56 * <th>参数名称</th>
57 * <th>描述</th>
58 * </tr>
59 * <tr>
60 * <td>sessionId</td>
61 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
62 * </tr>
63 * <tr>
64 * <td>sessionType</td>
65 * <td>会话类型,详见 SessionTypeEnum。</td>
66 * </tr>
67 * <tr>
68 * <td>text</td>
69 * <td>文本消息字符串,不得超过 5000 字符。</td>
70 * </tr>
71 * </table>
72 * @return
73 * {@link IMMessage} 对象。
74 *
75 */
76 public static IMMessage createTextMessage(String sessionId, SessionTypeEnum sessionType, String text) {
77 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
78 msg.setMsgType(MsgTypeEnum.text.getValue());
79 msg.setContent(text);
80 return msg;
81 }
82
83 /**
84 * 创建一条图片消息。
85 *
86 * @par 使用前提:
87 * 已登录 IM。
88 * @par 参数说明:
89 * <table>
90 * <tr>
91 * <th>参数名称</th>
92 * <th>描述</th>
93 * </tr>
94 * <tr>
95 * <td>sessionId</td>
96 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
97 * </tr>
98 * <tr>
99 * <td>sessionType</td>
100 * <td>会话类型,详见 SessionTypeEnum。</td>
101 * </tr>
102 * <tr>
103 * <td>file</td>
104 * <td>Android File 对象。</td>
105 * </tr>
106 * </table>
107 * @return
108 * {@link IMMessage} 对象。
109 *
110 */
111 public static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file) {
112 return createImageMessage(sessionId, sessionType, file, null, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
113 }
114
115 /**
116 * 创建一条图片消息。
117 *
118 * @par 使用前提:
119 * 已登录 IM。
120 * @par 参数说明:
121 * <table>
122 * <tr>
123 * <th>参数名称</th>
124 * <th>描述</th>
125 * </tr>
126 * <tr>
127 * <td>context</td>
128 * <td>Android Context 对象。</td>
129 * </tr>
130 * <tr>
131 * <td>sessionId</td>
132 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
133 * </tr>
134 * <tr>
135 * <td>sessionType</td>
136 * <td>会话类型,详见 SessionTypeEnum。</td>
137 * </tr>
138 * <tr>
139 * <td>uri</td>
140 * <td>图片文件 URI 地址。</td>
141 * </tr>
142 * </table>
143 * @return
144 * {@link IMMessage} 对象。
145 *
146 */
147 public static IMMessage createImageMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri) {
148 return createImageMessage(context,sessionId, sessionType, uri, null, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
149 }
150
151 /**
152 * 创建一条图片消息。
153 *
154 * @par 使用前提:
155 * 已登录 IM。
156 * @par 参数说明:
157 * <table>
158 * <tr>
159 * <th>参数名称</th>
160 * <th>描述</th>
161 * </tr>
162 * <tr>
163 * <td>sessionId</td>
164 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
165 * </tr>
166 * <tr>
167 * <td>sessionType</td>
168 * <td>会话类型,详见 SessionTypeEnum。</td>
169 * </tr>
170 * <tr>
171 * <td>file</td>
172 * <td>Android File 对象。</td>
173 * </tr>
174 * <tr>
175 * <td>displayName</td>
176 * <td>图片文件显示名称,可不同于文件名。</td>
177 * </tr>
178 * </table>
179 * @return
180 * {@link IMMessage} 对象。
181 *
182 */
183 public static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName) {
184 return createImageMessage(sessionId, sessionType, file, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
185 }
186
187 /**
188 * 创建一条图片消息。
189 *
190 * @par 使用前提:
191 * 已登录 IM。
192 * @par 参数说明:
193 * <table>
194 * <tr>
195 * <th>参数名称</th>
196 * <th>描述</th>
197 * </tr>
198 * <tr>
199 * <td>context</td>
200 * <td>Android Context 对象。</td>
201 * </tr>
202 * <tr>
203 * <td>sessionId</td>
204 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
205 * </tr>
206 * <tr>
207 * <td>sessionType</td>
208 * <td>会话类型,详见 SessionTypeEnum。</td>
209 * </tr>
210 * <tr>
211 * <td>uri</td>
212 * <td>图片文件 URI 地址。</td>
213 * </tr>
214 * <tr>
215 * <td>displayName</td>
216 * <td>图片文件显示名称,可不同于文件名。</td>
217 * </tr>
218 * </table>
219 * @return
220 * {@link IMMessage} 对象。
221 *
222 */
223 public static IMMessage createImageMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri,String displayName) {
224 return createImageMessage(context,sessionId, sessionType, uri, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
225 }
226
227 /**
228 * 创建一条图片消息。
229 *
230 * @par 使用前提:
231 * 已登录 IM。
232 * @par 参数说明:
233 * <table>
234 * <tr>
235 * <th>参数名称</th>
236 * <th>描述</th>
237 * </tr>
238 * <tr>
239 * <td>sessionId</td>
240 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
241 * </tr>
242 * <tr>
243 * <td>sessionType</td>
244 * <td>会话类型,详见 SessionTypeEnum。</td>
245 * </tr>
246 * <tr>
247 * <td>file</td>
248 * <td>Android File 对象。</td>
249 * </tr>
250 * <tr>
251 * <td>displayName</td>
252 * <td>图片文件显示名称,可不同于文件名。</td>
253 * </tr>
254 * <tr>
255 * <td>nosTokenSceneKey</td>
256 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
257 * </tr>
258 * </table>
259 * @return
260 * {@link IMMessage} 对象。
261 *
262 */
263 public static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName, String nosTokenSceneKey) {
264 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
265 msg.setMsgType(MsgTypeEnum.image.getValue());
266
267 final ImageAttachment attachment = createImageAttachment(file, displayName, nosTokenSceneKey);
268 msg.setAttachment(attachment);
269 return msg;
270 }
271
272 /**
273 * 创建一条图片消息。
274 *
275 * @par 使用前提:
276 * 已登录 IM。
277 * @par 参数说明:
278 * <table>
279 * <tr>
280 * <th>参数名称</th>
281 * <th>描述</th>
282 * </tr>
283 * <tr>
284 * <td>context</td>
285 * <td>Android Context 对象。</td>
286 * </tr>
287 * <tr>
288 * <td>sessionId</td>
289 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
290 * </tr>
291 * <tr>
292 * <td>sessionType</td>
293 * <td>会话类型,详见 SessionTypeEnum。</td>
294 * </tr>
295 * <tr>
296 * <td>uri</td>
297 * <td>图片文件 URI 地址。</td>
298 * </tr>
299 * <tr>
300 * <td>displayName</td>
301 * <td>图片文件显示名称,可不同于文件名。</td>
302 * </tr>
303 * <tr>
304 * <td>nosTokenSceneKey</td>
305 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
306 * </tr>
307 * </table>
308 * @return
309 * {@link IMMessage} 对象。
310 *
311 */
312 public static IMMessage createImageMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName, String nosTokenSceneKey) {
313 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
314 msg.setMsgType(MsgTypeEnum.image.getValue());
315 final ImageAttachment attachment = createImageAttachment(context, uri, displayName, nosTokenSceneKey);
316 msg.setAttachment(attachment);
317 return msg;
318 }
319
320 /**
321 * 创建一条语音消息。
322 *
323 * @par 使用前提:
324 * 已登录 IM。
325 * @par 参数说明:
326 * <table>
327 * <tr>
328 * <th>参数名称</th>
329 * <th>描述</th>
330 * </tr>
331 * <tr>
332 * <td>sessionId</td>
333 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
334 * </tr>
335 * <tr>
336 * <td>sessionType</td>
337 * <td>会话类型,详见 SessionTypeEnum。</td>
338 * </tr>
339 * <tr>
340 * <td>file</td>
341 * <td>Android File 对象。</td>
342 * </tr>
343 * <tr>
344 * <td>duration</td>
345 * <td>语音时长,单位为毫秒。</td>
346 * </tr>
347 * </table>
348 * @return
349 * {@link IMMessage} 对象。
350 *
351 */
352 public static IMMessage createAudioMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration) {
353 return createAudioMessage(sessionId, sessionType, file, duration, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
354 }
355
356 /**
357 * 创建一条语音消息。
358 *
359 * @par 使用前提:
360 * 已登录 IM。
361 * @par 参数说明:
362 * <table>
363 * <tr>
364 * <th>参数名称</th>
365 * <th>描述</th>
366 * </tr>
367 * <tr>
368 * <td>context</td>
369 * <td>Android Context 对象。</td>
370 * </tr>
371 * <tr>
372 * <td>sessionId</td>
373 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
374 * </tr>
375 * <tr>
376 * <td>sessionType</td>
377 * <td>会话类型,详见 SessionTypeEnum。</td>
378 * </tr>
379 * <tr>
380 * <td>uri</td>
381 * <td>音频文件 URI 地址。</td>
382 * </tr>
383 * <tr>
384 * <td>duration</td>
385 * <td>语音时长,单位为毫秒。</td>
386 * </tr>
387 * </table>
388 * @return
389 * {@link IMMessage} 对象。
390 *
391 */
392 public static IMMessage createAudioMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration) {
393 return createAudioMessage(context,sessionId, sessionType, uri, duration, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
394 }
395
396 /**
397 * 创建一条语音消息。
398 *
399 * @par 使用前提:
400 * 已登录 IM。
401 * @par 参数说明:
402 * <table>
403 * <tr>
404 * <th>参数名称</th>
405 * <th>描述</th>
406 * </tr>
407 * <tr>
408 * <td>sessionId</td>
409 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
410 * </tr>
411 * <tr>
412 * <td>sessionType</td>
413 * <td>会话类型,详见 SessionTypeEnum。</td>
414 * </tr>
415 * <tr>
416 * <td>file</td>
417 * <td>Android File 对象。</td>
418 * </tr>
419 * <tr>
420 * <td>duration</td>
421 * <td>语音时长,单位为毫秒。</td>
422 * </tr>
423 * <tr>
424 * <td>nosTokenSceneKey</td>
425 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
426 * </tr>
427 * </table>
428 * @return
429 * {@link IMMessage} 对象。
430 *
431 */
432 public static IMMessage createAudioMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, String nosTokenSceneKey) {
433 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
434 msg.setMsgType(MsgTypeEnum.audio.getValue());
435 if (duration > 0 && duration < 1000) {// 最低显示1秒
436 duration = 1000;
437 }
438 final AudioAttachment attachment = createAudioAttachment(file, duration, nosTokenSceneKey);
439 msg.setAttachment(attachment);
440 return msg;
441 }
442
443 /**
444 * 创建一条语音消息。
445 *
446 * @par 使用前提:
447 * 已登录 IM。
448 * @par 参数说明:
449 * <table>
450 * <tr>
451 * <th>参数名称</th>
452 * <th>描述</th>
453 * </tr>
454 * <tr>
455 * <td>context</td>
456 * <td>Android Context 对象。</td>
457 * </tr>
458 * <tr>
459 * <td>sessionId</td>
460 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
461 * </tr>
462 * <tr>
463 * <td>sessionType</td>
464 * <td>会话类型,详见 SessionTypeEnum。</td>
465 * </tr>
466 * <tr>
467 * <td>uri</td>
468 * <td>音频文件 URI 地址。</td>
469 * </tr>
470 * <tr>
471 * <td>duration</td>
472 * <td>语音时长,单位为毫秒。</td>
473 * </tr>
474 * <tr>
475 * <td>nosTokenSceneKey</td>
476 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
477 * </tr>
478 * </table>
479 * @return
480 * {@link IMMessage} 对象。
481 *
482 */
483 public static IMMessage createAudioMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, String nosTokenSceneKey) {
484 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
485 msg.setMsgType(MsgTypeEnum.audio.getValue());
486 if (duration > 0 && duration < 1000) {// 最低显示1秒
487 duration = 1000;
488 }
489 final AudioAttachment attachment = createAudioAttachment(context, uri, duration, nosTokenSceneKey);
490 msg.setAttachment(attachment);
491 return msg;
492 }
493
494 /**
495 * 创建一条地理位置消息。
496 *
497 * @par 使用前提:
498 * 已登录 IM。
499 * @par 参数说明:
500 * <table>
501 * <tr>
502 * <th>参数名称</th>
503 * <th>描述</th>
504 * </tr>
505 * <tr>
506 * <td>sessionId</td>
507 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
508 * </tr>
509 * <tr>
510 * <td>sessionType</td>
511 * <td>会话类型,详见 SessionTypeEnum。</td>
512 * </tr>
513 * <tr>
514 * <td>lat</td>
515 * <td>位置纬度。</td>
516 * </tr>
517 * <tr>
518 * <td>lng</td>
519 * <td>位置经度。</td>
520 * </tr>
521 * <tr>
522 * <td>addr</td>
523 * <td>位置描述信息。</td>
524 * </tr>
525 * </table>
526 * @return
527 * {@link IMMessage} 对象。
528 *
529 */
530 public static IMMessage createLocationMessage(String sessionId, SessionTypeEnum sessionType, double lat, double lng, String addr) {
531 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
532 msg.setMsgType(MsgTypeEnum.location.getValue());
533
534 final LocationAttachment location = new LocationAttachment();
535 location.setLatitude(lat);
536 location.setLongitude(lng);
537 location.setAddress(addr);
538 msg.setAttachStatus(AttachStatusEnum.transferred);
539 msg.setAttachment(location);
540
541 return msg;
542 }
543
544 /**
545 * 创建一条视频消息。
546 *
547 * @par 使用前提:
548 * 已登录 IM。
549 * @par 参数说明:
550 * <table>
551 * <tr>
552 * <th>参数名称</th>
553 * <th>描述</th>
554 * </tr>
555 * <tr>
556 * <td>sessionId</td>
557 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
558 * </tr>
559 * <tr>
560 * <td>sessionType</td>
561 * <td>会话类型,详见 SessionTypeEnum。</td>
562 * </tr>
563 * <tr>
564 * <td>file</td>
565 * <td>Android File 对象。</td>
566 * </tr>
567 * <tr>
568 * <td>duration</td>
569 * <td>视频文件时长,单位为毫秒。</td>
570 * </tr>
571 * <tr>
572 * <td>width</td>
573 * <td>视频宽度,单位为像素。</td>
574 * </tr>
575 * <tr>
576 * <td>height</td>
577 * <td>视频高度,单位为像素。</td>
578 * </tr>
579 * <tr>
580 * <td>displayName</td>
581 * <td>视频文件显示名称,可不同于文件名。</td>
582 * </tr>
583 * </table>
584 * @return
585 * {@link IMMessage} 对象。
586 *
587 */
588 public static IMMessage createVideoMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, int width, int height, String displayName) {
589 return createVideoMessage(sessionId, sessionType, file, duration, width, height, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
590 }
591
592 /**
593 * 创建一条视频消息。
594 *
595 * @par 使用前提:
596 * 已登录 IM。
597 * @par 参数说明:
598 * <table>
599 * <tr>
600 * <th>参数名称</th>
601 * <th>描述</th>
602 * </tr>
603 * <tr>
604 * <td>context</td>
605 * <td>Android Context 对象。</td>
606 * </tr>
607 * <tr>
608 * <td>sessionId</td>
609 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
610 * </tr>
611 * <tr>
612 * <td>sessionType</td>
613 * <td>会话类型,详见 SessionTypeEnum。</td>
614 * </tr>
615 * <tr>
616 * <td>uri</td>
617 * <td>视频文件 URI 地址。</td>
618 * </tr>
619 * <tr>
620 * <td>duration</td>
621 * <td>视频文件时长,单位为毫秒。</td>
622 * </tr>
623 * <tr>
624 * <td>width</td>
625 * <td>视频宽度,单位为像素。</td>
626 * </tr>
627 * <tr>
628 * <td>height</td>
629 * <td>视频高度,单位为像素。</td>
630 * </tr>
631 * <tr>
632 * <td>displayName</td>
633 * <td>视频文件显示名称,可不同于文件名。</td>
634 * </tr>
635 * </table>
636 * @return
637 * {@link IMMessage} 对象。
638 *
639 */
640 @SuppressWarnings("java:S107")
641 public static IMMessage createVideoMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, int width, int height, String displayName) {
642 return createVideoMessage(context,sessionId, sessionType, uri, duration, width, height, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
643 }
644
645 /**
646 * 创建一条视频消息。
647 *
648 * @par 使用前提:
649 * 已登录 IM。
650 * @par 参数说明:
651 * <table>
652 * <tr>
653 * <th>参数名称</th>
654 * <th>描述</th>
655 * </tr>
656 * <tr>
657 * <td>sessionId</td>
658 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
659 * </tr>
660 * <tr>
661 * <td>sessionType</td>
662 * <td>会话类型,详见 SessionTypeEnum。</td>
663 * </tr>
664 * <tr>
665 * <td>file</td>
666 * <td>Android File 对象。</td>
667 * </tr>
668 * <tr>
669 * <td>duration</td>
670 * <td>视频文件时长,单位为毫秒。</td>
671 * </tr>
672 * <tr>
673 * <td>width</td>
674 * <td>视频宽度,单位为像素。</td>
675 * </tr>
676 * <tr>
677 * <td>height</td>
678 * <td>视频高度,单位为像素。</td>
679 * </tr>
680 * <tr>
681 * <td>displayName</td>
682 * <td>视频文件显示名称,可不同于文件名。</td>
683 * </tr>
684 * <tr>
685 * <td>nosTokenSceneKey</td>
686 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
687 * </tr>
688 * </table>
689 * @return
690 * {@link IMMessage} 对象。
691 *
692 */
693 public static IMMessage createVideoMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
694 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
695 msg.setMsgType(MsgTypeEnum.video.getValue());
696
697 final VideoAttachment attachment = createVideoAttachment(file, duration, width, height, displayName, nosTokenSceneKey);
698 msg.setAttachment(attachment);
699
700 BitmapDecoder.extractThumbnail(file.getPath(), attachment.getThumbPathForSave());
701
702 return msg;
703 }
704
705 /**
706 * 创建一条视频消息。
707 *
708 * @par 使用前提:
709 * 已登录 IM。
710 * @par 参数说明:
711 * <table>
712 * <tr>
713 * <th>参数名称</th>
714 * <th>描述</th>
715 * </tr>
716 * <tr>
717 * <td>context</td>
718 * <td>Android Context 对象。</td>
719 * </tr>
720 * <tr>
721 * <td>sessionId</td>
722 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
723 * </tr>
724 * <tr>
725 * <td>sessionType</td>
726 * <td>会话类型,详见 SessionTypeEnum。</td>
727 * </tr>
728 * <tr>
729 * <td>uri</td>
730 * <td>视频文件 URI 地址。</td>
731 * </tr>
732 * <tr>
733 * <td>duration</td>
734 * <td>视频文件时长,单位为毫秒。</td>
735 * </tr>
736 * <tr>
737 * <td>width</td>
738 * <td>视频宽度,单位为像素。</td>
739 * </tr>
740 * <tr>
741 * <td>height</td>
742 * <td>视频高度,单位为像素。</td>
743 * </tr>
744 * <tr>
745 * <td>displayName</td>
746 * <td>视频文件显示名称,可不同于文件名。</td>
747 * </tr>
748 * <tr>
749 * <td>nosTokenSceneKey</td>
750 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
751 * </tr>
752 * </table>
753 * @return
754 * {@link IMMessage} 对象。
755 *
756 */
757 @SuppressWarnings("java:S107")
758 public static IMMessage createVideoMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
759 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
760 msg.setMsgType(MsgTypeEnum.video.getValue());
761 final VideoAttachment attachment = createVideoAttachment(context, uri, duration, width, height, displayName, nosTokenSceneKey);
762 msg.setAttachment(attachment);
763
764 BitmapDecoder.extractThumbnail(uri, attachment.getThumbPathForSave());
765
766 return msg;
767 }
768
769 /**
770 * 创建一条文件消息。
771 *
772 * @par 使用前提:
773 * 已登录 IM。
774 * @par 参数说明:
775 * <table>
776 * <tr>
777 * <th>参数名称</th>
778 * <th>描述</th>
779 * </tr>
780 * <tr>
781 * <td>sessionId</td>
782 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
783 * </tr>
784 * <tr>
785 * <td>sessionType</td>
786 * <td>会话类型,详见 SessionTypeEnum。</td>
787 * </tr>
788 * <tr>
789 * <td>file</td>
790 * <td>Android File 对象。</td>
791 * </tr>
792 * <tr>
793 * <td>displayName</td>
794 * <td>文件显示名称,可不同于文件名。</td>
795 * </tr>
796 * </table>
797 * @return
798 * {@link IMMessage} 对象。
799 *
800 */
801 public static IMMessage createFileMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName) {
802 return createFileMessage(sessionId, sessionType, file, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
803 }
804
805 /**
806 * 创建一条文件消息。
807 *
808 * @par 使用前提:
809 * 已登录 IM。
810 * @par 参数说明:
811 * <table>
812 * <tr>
813 * <th>参数名称</th>
814 * <th>描述</th>
815 * </tr>
816 * <tr>
817 * <td>sessionId</td>
818 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
819 * </tr>
820 * <tr>
821 * <td>sessionType</td>
822 * <td>会话类型,详见 SessionTypeEnum。</td>
823 * </tr>
824 * <tr>
825 * <td>uri</td>
826 * <td>文件 URI 地址。</td>
827 * </tr>
828 * <tr>
829 * <td>displayName</td>
830 * <td>文件显示名称,可不同于文件名。</td>
831 * </tr>
832 * </table>
833 * @return
834 * {@link IMMessage} 对象。
835 *
836 */
837 public static IMMessage createFileMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName) {
838 return createFileMessage(context,sessionId, sessionType, uri, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
839 }
840
841 /**
842 * 创建一条文件消息。
843 *
844 * @par 使用前提:
845 * 已登录 IM。
846 * @par 参数说明:
847 * <table>
848 * <tr>
849 * <th>参数名称</th>
850 * <th>描述</th>
851 * </tr>
852 * <tr>
853 * <td>sessionId</td>
854 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
855 * </tr>
856 * <tr>
857 * <td>sessionType</td>
858 * <td>会话类型,详见 SessionTypeEnum。</td>
859 * </tr>
860 * <tr>
861 * <td>file</td>
862 * <td>Android File 对象。</td>
863 * </tr>
864 * <tr>
865 * <td>displayName</td>
866 * <td>文件显示名称,可不同于文件名。</td>
867 * </tr>
868 * <tr>
869 * <td>nosTokenSceneKey</td>
870 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
871 * </tr>
872 * </table>
873 * @return
874 * {@link IMMessage} 对象。
875 *
876 */
877 public static IMMessage createFileMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName, String nosTokenSceneKey) {
878 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
879 msg.setMsgType(MsgTypeEnum.file.getValue());
880
881 final FileAttachment attachment = createFileAttachment(file, displayName, nosTokenSceneKey);
882 msg.setAttachment(attachment);
883 return msg;
884 }
885
886 /**
887 * 创建一条文件消息。
888 *
889 * @par 使用前提:
890 * 已登录 IM。
891 * @par 参数说明:
892 * <table>
893 * <tr>
894 * <th>参数名称</th>
895 * <th>描述</th>
896 * </tr>
897 * <tr>
898 * <td>context</td>
899 * <td>Android Context 对象。</td>
900 * </tr>
901 * <tr>
902 * <td>sessionId</td>
903 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
904 * </tr>
905 * <tr>
906 * <td>sessionType</td>
907 * <td>会话类型,详见 SessionTypeEnum。</td>
908 * </tr>
909 * <tr>
910 * <td>uri</td>
911 * <td>文件 URI 地址。</td>
912 * </tr>
913 * <tr>
914 * <td>displayName</td>
915 * <td>文件显示名称,可不同于文件名。</td>
916 * </tr>
917 * <tr>
918 * <td>nosTokenSceneKey</td>
919 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
920 * </tr>
921 * </table>
922 * @return
923 * {@link IMMessage} 对象。
924 *
925 */
926 public static IMMessage createFileMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName, String nosTokenSceneKey) {
927 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
928 msg.setMsgType(MsgTypeEnum.file.getValue());
929 final FileAttachment attachment = createFileAttachment(context, uri, displayName, nosTokenSceneKey);
930 msg.setAttachment(attachment);
931 return msg;
932 }
933
934 /**
935 * 创建一条提醒消息。
936 *
937 * @par 使用前提:
938 * 已登录 IM。
939 * @par 参数说明:
940 * <table>
941 * <tr>
942 * <th>参数名称</th>
943 * <th>描述</th>
944 * </tr>
945 * <tr>
946 * <td>sessionId</td>
947 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
948 * </tr>
949 * <tr>
950 * <td>sessionType</td>
951 * <td>会话类型,详见 SessionTypeEnum。</td>
952 * </tr>
953 * </table>
954 * @return
955 * {@link IMMessage} 对象。
956 *
957 */
958 public static IMMessage createTipMessage(String sessionId, SessionTypeEnum sessionType) {
959 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
960 msg.setMsgType(MsgTypeEnum.tip.getValue());
961
962 return msg;
963 }
964
965 /**
966 * 创建一条机器人普通文本消息。
967 *
968 * @par 使用前提:
969 * 已登录 IM。
970 * @par 参数说明:
971 * <table>
972 * <tr>
973 * <th>参数名称</th>
974 * <th>描述</th>
975 * </tr>
976 * <tr>
977 * <td>sessionId</td>
978 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
979 * </tr>
980 * <tr>
981 * <td>sessionType</td>
982 * <td>会话类型,详见 SessionTypeEnum。</td>
983 * </tr>
984 * <tr>
985 * <td>text</td>
986 * <td>文本消息内容。</td>
987 * </tr>
988 * <tr>
989 * <td>messageRobotInfo</td>
990 * <td>{@link MessageRobotInfo}</td>
991 * </tr>
992 * </table>
993 * @return
994 * {@link IMMessage} 对象。
995 *
996 */
997 public static IMMessage createRobotTextMessage(String sessionId, SessionTypeEnum sessionType, String text, MessageRobotInfo messageRobotInfo) {
998 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
999 msg.setMsgType(MsgTypeEnum.text.getValue());
1000 msg.setContent(text);
1001 msg.setRobotInfo(messageRobotInfo);
1002 return msg;
1003 }
1004
1005 /**
1006 * 创建一条机器人提醒消息。
1007 *
1008 * @par 使用前提:
1009 * 已登录 IM。
1010 * @par 参数说明:
1011 * <table>
1012 * <tr>
1013 * <th>参数名称</th>
1014 * <th>描述</th>
1015 * </tr>
1016 * <tr>
1017 * <td>sessionId</td>
1018 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1019 * </tr>
1020 * <tr>
1021 * <td>sessionType</td>
1022 * <td>会话类型,详见 SessionTypeEnum。</td>
1023 * </tr>
1024 * <tr>
1025 * <td>messageRobotInfo</td>
1026 * <td>{@link MessageRobotInfo}</td>
1027 * </tr>
1028 * </table>
1029 * @return
1030 * {@link IMMessage} 对象。
1031 *
1032 */
1033 public static IMMessage createRobotTipMessage(String sessionId, SessionTypeEnum sessionType, MessageRobotInfo messageRobotInfo) {
1034 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1035 msg.setMsgType(MsgTypeEnum.tip.getValue());
1036 msg.setRobotInfo(messageRobotInfo);
1037 return msg;
1038 }
1039
1040 /**
1041 * 创建一条自定义消息。
1042 *
1043 * @par 使用前提:
1044 * <ul><li>已登录 IM。</li><li>已注册一个自定义消息解析器。详见[自定义消息收发](https://doc.yunxin.163.com/messaging/docs/DY3Mjg5NjE?platform=android#%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95)。</li></ul>
1045 * @par 注意事项:
1046 * <ul><li>NIM SDK 不负责定义和解析自定义消息的具体内容,您需要自行完成解析。</li><li>SDK 会将自定义消息存入消息数据库,与内置消息一并展示在消息记录中。</li></ul>
1047 * @par 参数说明:
1048 * <table>
1049 * <tr>
1050 * <th>参数名称</th>
1051 * <th>描述</th>
1052 * </tr>
1053 * <tr>
1054 * <td>sessionId</td>
1055 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1056 * </tr>
1057 * <tr>
1058 * <td>sessionType</td>
1059 * <td>会话类型,详见 SessionTypeEnum。</td>
1060 * </tr>
1061 * <tr>
1062 * <td>attachment</td>
1063 * <td>MsgAttachment 对象。长度不得超过 5000 字符。</td>
1064 * </tr>
1065 * </table>
1066 * @return
1067 * {@link IMMessage} 对象。
1068 *
1069 */
1070 public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, MsgAttachment attachment) {
1071 return createCustomMessage(sessionId, sessionType, null, attachment, null);
1072 }
1073
1074 /**
1075 * 创建一条自定义消息。
1076 *
1077 * @par 使用前提:
1078 * <ul><li>已登录 IM。</li><li>已注册一个自定义消息解析器。详见[自定义消息收发](https://doc.yunxin.163.com/messaging/docs/DY3Mjg5NjE?platform=android#%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95)。</li></ul>
1079 * @par 注意事项:
1080 * <ul><li>NIM SDK 不负责定义和解析自定义消息的具体内容,您需要自行完成解析。</li><li>SDK 会将自定义消息存入消息数据库,与内置消息一并展示在消息记录中。</li></ul>
1081 * @par 参数说明:
1082 * <table>
1083 * <tr>
1084 * <th>参数名称</th>
1085 * <th>描述</th>
1086 * </tr>
1087 * <tr>
1088 * <td>sessionId</td>
1089 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1090 * </tr>
1091 * <tr>
1092 * <td>sessionType</td>
1093 * <td>会话类型,详见 SessionTypeEnum。</td>
1094 * </tr>
1095 * <tr>
1096 * <td>content</td>
1097 * <td>消息简要描述,可用于推送及状态栏消息提醒的展示。创建成功后可通过 {@link IMMessage#getContent} 获取。</td>
1098 * </tr>
1099 * <tr>
1100 * <td>attachment</td>
1101 * <td>MsgAttachment 对象。长度不得超过 5000 字符。</td>
1102 * </tr>
1103 * </table>
1104 * @return
1105 * {@link IMMessage} 对象。
1106 *
1107 */
1108 public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment) {
1109 return createCustomMessage(sessionId, sessionType, content, attachment, null);
1110 }
1111
1112 /**
1113 * 创建一条自定义消息。
1114 *
1115 * @par 使用前提:
1116 * <ul><li>已登录 IM。</li><li>已注册一个自定义消息解析器。详见[自定义消息收发](https://doc.yunxin.163.com/messaging/docs/DY3Mjg5NjE?platform=android#%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95)。</li></ul>
1117 * @par 注意事项:
1118 * <ul><li>NIM SDK 不负责定义和解析自定义消息的具体内容,您需要自行完成解析。</li><li>SDK 会将自定义消息存入消息数据库,与内置消息一并展示在消息记录中。</li></ul>
1119 * @par 参数说明:
1120 * <table>
1121 * <tr>
1122 * <th>参数名称</th>
1123 * <th>描述</th>
1124 * </tr>
1125 * <tr>
1126 * <td>sessionId</td>
1127 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1128 * </tr>
1129 * <tr>
1130 * <td>sessionType</td>
1131 * <td>会话类型,详见 SessionTypeEnum。</td>
1132 * </tr>
1133 * <tr>
1134 * <td>content</td>
1135 * <td>消息简要描述,可用于推送及状态栏消息提醒的展示。创建成功后可通过 {@link IMMessage#getContent} 获取。</td>
1136 * </tr>
1137 * <tr>
1138 * <td>attachment</td>
1139 * <td>MsgAttachment 对象。长度不得超过 5000 字符。</td>
1140 * </tr>
1141 * <tr>
1142 * <td>config</td>
1143 * <td>自定义消息配置,详见 CustomMessageConfig。用于设定消息的声明周期,是否需要推送,是否需要计入未读数等。</td>
1144 * </tr>
1145 * </table>
1146 * @return
1147 * {@link IMMessage} 对象。
1148 *
1149 */
1150 public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment, CustomMessageConfig config) {
1151 return createCustomMessage(sessionId, sessionType, content, attachment, config, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
1152 }
1153
1154 /**
1155 * 创建一条自定义消息。
1156 *
1157 * @par 使用前提:
1158 * <ul><li>已登录 IM。</li><li>已注册一个自定义消息解析器。详见[自定义消息收发](https://doc.yunxin.163.com/messaging/docs/DY3Mjg5NjE?platform=android#%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95)。</li></ul>
1159 * @par 注意事项:
1160 * <ul><li>NIM SDK 不负责定义和解析自定义消息的具体内容,您需要自行完成解析。</li><li>SDK 会将自定义消息存入消息数据库,与内置消息一并展示在消息记录中。</li></ul>
1161 * @par 参数说明:
1162 * <table>
1163 * <tr>
1164 * <th>参数名称</th>
1165 * <th>描述</th>
1166 * </tr>
1167 * <tr>
1168 * <td>sessionId</td>
1169 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1170 * </tr>
1171 * <tr>
1172 * <td>sessionType</td>
1173 * <td>会话类型,详见 SessionTypeEnum。</td>
1174 * </tr>
1175 * <tr>
1176 * <td>content</td>
1177 * <td>消息简要描述,可用于推送及状态栏消息提醒的展示。创建成功后可通过 {@link IMMessage#getContent} 获取。</td>
1178 * </tr>
1179 * <tr>
1180 * <td>attachment</td>
1181 * <td>MsgAttachment 对象。长度不得超过 5000 字符。</td>
1182 * </tr>
1183 * <tr>
1184 * <td>config</td>
1185 * <td>自定义消息配置,详见 CustomMessageConfig。用于设定消息的声明周期,是否需要推送,是否需要计入未读数等。</td>
1186 * </tr>
1187 * <tr>
1188 * <td>nosTokenSceneKey</td>
1189 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1190 * </tr>
1191 * </table>
1192 * @return
1193 * {@link IMMessage} 对象。
1194 *
1195 */
1196 public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment, CustomMessageConfig config, String nosTokenSceneKey) {
1197 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1198 msg.setMsgType(MsgTypeEnum.custom.getValue());
1199 msg.setContent(content);
1200 if (attachment != null && attachment instanceof FileAttachment) {
1201 ((FileAttachment) attachment).setNosTokenSceneKey(nosTokenSceneKey);
1202 }
1203 msg.setAttachment(attachment);
1204 msg.setConfig(config);
1205 return msg;
1206 }
1207
1208 /**
1209 * 该方法不推荐使用,请使用 {@link MessageBuilder#createRobotTextMessage} 和 {@link MessageBuilder#createRobotTipMessage}。
1210 */
1211 public static IMMessage createRobotMessage(String sessionId, SessionTypeEnum sessionType, String robotAccount, String text, String type, String content, String target, String params) {
1212 if (TextUtils.isEmpty(type) || TextUtils.isEmpty(robotAccount)) {
1213 throw new IllegalArgumentException("Invalid param, type and robot account should not be null");
1214 }
1215
1216 if (type.equals(RobotMsgType.TEXT) && content == null) {
1217 throw new IllegalArgumentException("Invalid param, content should not be null");
1218 } else if (type.equals(RobotMsgType.LINK) && TextUtils.isEmpty(target)) {
1219 throw new IllegalArgumentException("Invalid param, target should not be null");
1220 }
1221
1222 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1223 msg.setMsgType(MsgTypeEnum.robot.getValue());
1224
1225 // build attachment
1226 RobotAttachment robotAttachment = new RobotAttachment();
1227 robotAttachment.initSend(robotAccount, type, content, target, params);
1228 msg.setAttachment(robotAttachment);
1229 msg.setContent(text); // 文案显示用,便于全文检索等
1230
1231 return msg;
1232 }
1233
1234 /**
1235 * 创建一条通话记录消息。
1236 *
1237 * @par 使用前提:
1238 * 已登录 IM。
1239 * @par 参数说明:
1240 * <table>
1241 * <tr>
1242 * <th>参数名称</th>
1243 * <th>描述</th>
1244 * </tr>
1245 * <tr>
1246 * <td>sessionId</td>
1247 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1248 * </tr>
1249 * <tr>
1250 * <td>sessionType</td>
1251 * <td>会话类型,详见 SessionTypeEnum。</td>
1252 * </tr>
1253 * <tr>
1254 * <td>netCallAttachment</td>
1255 * <td>通话记录附件对象,详见 MsgAttachment。</td>
1256 * </tr>
1257 * </table>
1258 * @return
1259 * {@link IMMessage} 对象。
1260 *
1261 */
1262 public static IMMessage createNrtcNetcallMessage(String sessionId, SessionTypeEnum sessionType, MsgAttachment netCallAttachment) {
1263 IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1264 msg.setMsgType(MsgTypeEnum.nrtc_netcall.getValue());
1265 msg.setAttachment(netCallAttachment);
1266 return msg;
1267 }
1268
1269 /**
1270 * 创建一条空消息。
1271 *
1272 *该方法仅设置聊天对象以及时间点,用于历史消息查询。
1273 *
1274 * @par 使用前提:
1275 * 已登录 IM。
1276 * @par 参数说明:
1277 * <table>
1278 * <tr>
1279 * <th>参数名称</th>
1280 * <th>描述</th>
1281 * </tr>
1282 * <tr>
1283 * <td>sessionId</td>
1284 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1285 * </tr>
1286 * <tr>
1287 * <td>sessionType</td>
1288 * <td>会话类型,详见 SessionTypeEnum。</td>
1289 * </tr>
1290 * <tr>
1291 * <td>time</td>
1292 * <td>历史消息查询的起始时间 Unix 时间戳(毫秒)。</td>
1293 * </tr>
1294 * </table>
1295 * @return
1296 * {@link IMMessage} 对象。
1297 *
1298 */
1299 public static IMMessage createEmptyMessage(String sessionId, SessionTypeEnum sessionType, long time) {
1300 IMMessageImpl msg = new IMMessageImpl();
1301 msg.setSessionId(sessionId);
1302 msg.setSessionType(sessionType);
1303 msg.setTime(time);
1304 return msg;
1305 }
1306
1307 /**
1308 * 创建一条待转发消息。
1309 *
1310 * @par 使用前提:
1311 * 已登录 IM 并实现消息发送和接收。
1312 * @par 参数说明:
1313 * <table>
1314 * <tr>
1315 * <th>参数名称</th>
1316 * <th>描述</th>
1317 * </tr>
1318 * <tr>
1319 * <td>message</td>
1320 * <td>已创建的 {@link IMMessage} 对象。</td>
1321 * </tr>
1322 * <tr>
1323 * <td>sessionId</td>
1324 * <td>会话 ID,根据会话类型(sessionType)判断:<ul><li>如果是单聊,则 sessionId 为用户的云信 IM 帐号(即 accid)</li><li>如果是群聊,则 sessionId 为群组 ID</li></ul></td>
1325 * </tr>
1326 * <tr>
1327 * <td>sessionType</td>
1328 * <td>会话类型,详见 SessionTypeEnum。</td>
1329 * </tr>
1330 * <tr>
1331 * <td>time</td>
1332 * <td>历史消息查询的起始时间 Unix 时间戳(毫秒)。</td>
1333 * </tr>
1334 * </table>
1335 * @return
1336 * 待转发的 {@link IMMessage} 对象。
1337 *
1338 */
1339 public static IMMessage createForwardMessage(IMMessage message, String sessionId, SessionTypeEnum sessionType) {
1340 if (message.getMsgType() == MsgTypeEnum.notification || message.getMsgType() == MsgTypeEnum.avchat || message.getMsgType() == MsgTypeEnum.robot) {
1341 return null;
1342 }
1343 IMMessageImpl forward = ((IMMessageImpl) message).deepClone();
1344 if (forward != null) {
1345 forward.setSessionId(sessionId);
1346 forward.setSessionType(sessionType);
1347 forward.setUuid(StringUtil.get32UUID());
1348 forward.setFromAccount(SDKCacheUI.getAccount());
1349 forward.setDirect(MsgDirectionEnum.Out);
1350 forward.setStatus(MsgStatusEnum.sending);
1351 forward.setTime(TimeUtil.currentTimeMillis());
1352 forward.setServerId(0L);
1353 forward.setMessageId(0L);
1354 // clear team msg ack
1355 forward.setMsgAck(false);
1356 forward.setHasSendAck(false);
1357 forward.setTeamMsgAckCount(0);
1358 forward.setTeamMsgUnAckCount(0);
1359
1360 // 接收到消息,但是附件没有下载成功的情况。不会再次上传附件,所以要将附件的发送状态直接改为def
1361 final MsgAttachment attachment = forward.getAttachment();
1362 if (attachment != null && attachment instanceof FileAttachment) {
1363 final FileAttachment media = (FileAttachment) attachment;
1364 if (!TextUtils.isEmpty(media.getUrl())) {
1365 forward.setAttachStatus(AttachStatusEnum.def);
1366 }
1367 }
1368 }
1369
1370 return forward;
1371 }
1372
1373 private static IMMessageImpl initSendMessage(String toId, SessionTypeEnum sessionType) {
1374 IMMessageImpl msg = new IMMessageImpl();
1375
1376 msg.setUuid(StringUtil.get32UUID());
1377 msg.setSessionId(toId);
1378 msg.setFromAccount(SDKCacheUI.getAccount());
1379 msg.setDirect(MsgDirectionEnum.Out);
1380 msg.setStatus(MsgStatusEnum.sending);
1381 msg.setSessionType(sessionType);
1382 msg.setTime(TimeUtil.currentTimeMillis());
1383
1384 return msg;
1385 }
1386
1387 /**
1388 * 创建多条待合并转发的消息。
1389 *
1390 * @par 使用前提:
1391 * 已登录 IM 并实现消息发送和接收。
1392 * @par 参数说明:
1393 * <table>
1394 * <tr>
1395 * <th>参数名称</th>
1396 * <th>描述</th>
1397 * </tr>
1398 * <tr>
1399 * <td>message</td>
1400 * <td>已创建的 {@link IMMessage} 列表。</td>
1401 * </tr>
1402 * </table>
1403 * @return
1404 * 待转发的多条消息字符串。
1405 *
1406 */
1407 public static String createForwardMessageListFileDetail(List<? extends IMMessage> messages) {
1408 if (messages == null || messages.isEmpty()) {
1409 return "";
1410 }
1411 final String sessionId = messages.get(0).getSessionId();
1412 if (sessionId==null) {
1413 return "";
1414 }
1415 List<IMMessage> msgList = new ArrayList<>(messages.size());
1416 for (IMMessage msg : messages) {
1417 // 不是来自同一会话,不添加
1418 if (!sessionId.equals(msg.getSessionId())){
1419 continue;
1420 }
1421 // 不是可以转发的类型,不添加
1422 if (msg.getMsgType() == MsgTypeEnum.notification || msg.getMsgType() == MsgTypeEnum.avchat || msg.getMsgType() == MsgTypeEnum.robot) {
1423 continue;
1424 }
1425 msgList.add(msg);
1426 }
1427 return buildHeader(0, msgList.size()) + "\n" + buildBody(msgList);
1428 }
1429
1430 /**
1431 * 序列化文件的头部,占一行
1432 *
1433 * @param version 文件格式版本
1434 * @param message_count 消息数目
1435 * @return 头部字符串
1436 */
1437 private static String buildHeader(int version, int message_count) {
1438 JSONObject obj = new JSONObject();
1439 try {
1440 obj.put("version", version);
1441 //终端类型:Android
1442 obj.put("terminal", IAuthService.ClientType.Android);
1443 obj.put("sdk_version", BuildConfig.VERSION_NAME);
1444 obj.put("app_version", BuildConfig.VERSION_CODE);
1445 obj.put("message_count", message_count);
1446 } catch (JSONException e) {
1447 e.printStackTrace();
1448 }
1449 return obj.toString();
1450 }
1451
1452 /**
1453 * 把需要进行上传的数据 按照约定的“数据字段字典”转换后的json格式字符串,每条记录一行,存储在文本文件中,并从第二行开始存储(因为第一行存的是head信息)
1454 *
1455 * @param msgList 要合并的消息列表
1456 * @return 合并后的字符串
1457 */
1458 private static String buildBody(@NonNull List<IMMessage> msgList) {
1459 String enter = "\n";
1460 if (msgList.isEmpty()) {
1461 return "";
1462 }
1463 StringBuilder stringBuilder = new StringBuilder();
1464 for (IMMessage msg : msgList) {
1465 stringBuilder.append(enter).append(MessageConvert.convertMessageToJson(msg, SDKCacheUI.getAccount()));
1466 }
1467 return stringBuilder.substring(enter.length());
1468 }
1469
1470
1471 /**
1472 * 创建一条 JSON 消息。
1473 *
1474 * @par 使用前提:
1475 * 已登录 IM。
1476 * @par 参数说明:
1477 * <table>
1478 * <tr>
1479 * <th>参数名称</th>
1480 * <th>描述</th>
1481 * </tr>
1482 * <tr>
1483 * <td>json</td>
1484 * <td>JSON 格式的字符串。</td>
1485 * </tr>
1486 * </table>
1487 * @return
1488 * {@link IMMessage} 对象。
1489 *
1490 */
1491 public static IMMessage createFromJson(String json){
1492 if (TextUtils.isEmpty(json)) {
1493 return null;
1494 }
1495 return MessageConvert.convertJsonToMessage(json, SDKCacheUI.getAccount());
1496 }
1497
1498 /**
1499 * 将 IMMessage 对象转换成 JSON 格式的字符串。
1500 *
1501 * @par 使用前提:
1502 * 已调用 createXXMessage 创建一条消息,并返回 {@link IMMessage} 对象。
1503 * @par 参数说明:
1504 * <table>
1505 * <tr>
1506 * <th>参数名称</th>
1507 * <th>描述</th>
1508 * </tr>
1509 * <tr>
1510 * <td>msg</td>
1511 * <td>{@link IMMessage} 消息体。</td>
1512 * </tr>
1513 * </table>
1514 * @return
1515 * 转换后的 JSON 字符串。
1516 *
1517 */
1518 public static String convertMessageToJson(IMMessage msg) {
1519 if (msg == null) {
1520 return null;
1521 }
1522 return MessageConvert.convertMessageToJson(msg, SDKCacheUI.getAccount());
1523 }
1524
1525 /**
1526 * 创建图片附件,用于上传图片文件。
1527 *
1528 * @par 使用前提:
1529 * 已登录 IM。
1530 * @par 参数说明:
1531 * <table>
1532 * <tr>
1533 * <th>参数名称</th>
1534 * <th>描述</th>
1535 * </tr>
1536 * <tr>
1537 * <td>file</td>
1538 * <td>Android File 对象。</td>
1539 * </tr>
1540 * <tr>
1541 * <td>displayName</td>
1542 * <td>图片文件显示名称,可不同于文件名。</td>
1543 * </tr>
1544 * <tr>
1545 * <td>nosTokenSceneKey</td>
1546 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1547 * </tr>
1548 * </table>
1549 * @return
1550 * ImageAttachment 对象。
1551 *
1552 */
1553 @androidx.annotation.NonNull
1554 public static ImageAttachment createImageAttachment(File file, String displayName, String nosTokenSceneKey) {
1555 final ImageAttachment attachment = new ImageAttachment();
1556 attachment.setPath(file.getPath());
1557 attachment.setSize(file.length());
1558 int[] dimension = BitmapDecoder.decodeBound(file);
1559 attachment.setWidth(dimension[0]);
1560 attachment.setHeight(dimension[1]);
1561 attachment.setDisplayName(displayName);
1562 attachment.setExtension(StringUtil.getExtension(file.getName()));
1563 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1564 return attachment;
1565 }
1566
1567 /**
1568 * 创建图片附件,用于上传图片文件。
1569 *
1570 * @par 使用前提:
1571 * 已登录 IM。
1572 * @par 参数说明:
1573 * <table>
1574 * <tr>
1575 * <th>参数名称</th>
1576 * <th>描述</th>
1577 * </tr>
1578 * <tr>
1579 * <td>context</td>
1580 * <td>Android Context 对象。</td>
1581 * </tr>
1582 * <tr>
1583 * <td>uri</td>
1584 * <td>图片文件 URI 地址。</td>
1585 * </tr>
1586 * <tr>
1587 * <td>displayName</td>
1588 * <td>图片文件显示名称,可不同于文件名。</td>
1589 * </tr>
1590 * <tr>
1591 * <td>nosTokenSceneKey</td>
1592 * <td>图片文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1593 * </tr>
1594 * </table>
1595 * @return
1596 * ImageAttachment 对象。
1597 *
1598 */
1599 @androidx.annotation.NonNull
1600 public static ImageAttachment createImageAttachment(Context context, Uri uri, String displayName, String nosTokenSceneKey) {
1601 final ImageAttachment attachment = new ImageAttachment();
1602 attachment.setUri(uri);
1603 attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1604 int[] dimension = BitmapDecoder.decodeBound(context, uri);
1605 attachment.setWidth(dimension[0]);
1606 attachment.setHeight(dimension[1]);
1607 attachment.setDisplayName(displayName);
1608 attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1609 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1610 return attachment;
1611 }
1612
1613 /**
1614 * 创建音频附件。用于上传音频文件。
1615 *
1616 * @par 使用前提:
1617 * 已登录 IM。
1618 * @par 参数说明:
1619 * <table>
1620 * <tr>
1621 * <th>参数名称</th>
1622 * <th>描述</th>
1623 * </tr>
1624 * <tr>
1625 * <td>file</td>
1626 * <td>Android File 对象。</td>
1627 * </tr>
1628 * <tr>
1629 * <td>duration</td>
1630 * <td>音频文件时长,单位为毫秒。</td>
1631 * </tr>
1632 * <tr>
1633 * <td>nosTokenSceneKey</td>
1634 * <td>音频文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1635 * </tr>
1636 * </table>
1637 * @return
1638 * AudioAttachment 对象。
1639 *
1640 */
1641 @androidx.annotation.NonNull
1642 public static AudioAttachment createAudioAttachment(File file, long duration, String nosTokenSceneKey) {
1643 final AudioAttachment attachment = new AudioAttachment();
1644 attachment.setPath(file.getPath());
1645 attachment.setSize(file.length());
1646 attachment.setDuration(duration);// ms
1647 attachment.setExtension(StringUtil.getExtension(file.getName()));
1648 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1649 return attachment;
1650 }
1651
1652 /**
1653 * 创建音频附件。用于上传音频文件。
1654 *
1655 * @par 使用前提:
1656 * 已登录 IM。
1657 * @par 参数说明:
1658 * <table>
1659 * <tr>
1660 * <th>参数名称</th>
1661 * <th>描述</th>
1662 * </tr>
1663 * <tr>
1664 * <td>context</td>
1665 * <td>Android Context 对象。</td>
1666 * </tr>
1667 * <tr>
1668 * <td>uri</td>
1669 * <td>音频文件 URI 地址。</td>
1670 * </tr>
1671 * <tr>
1672 * <td>duration</td>
1673 * <td>音频文件时长,单位为毫秒。</td>
1674 * </tr>
1675 * <tr>
1676 * <td>nosTokenSceneKey</td>
1677 * <td>音频文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1678 * </tr>
1679 * </table>
1680 * @return
1681 * AudioAttachment 对象。
1682 *
1683 */
1684 @androidx.annotation.NonNull
1685 public static AudioAttachment createAudioAttachment(Context context, Uri uri, long duration, String nosTokenSceneKey) {
1686 final AudioAttachment attachment = new AudioAttachment();
1687 attachment.setUri(uri);
1688 attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1689 attachment.setDuration(duration);// ms
1690 attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1691 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1692 return attachment;
1693 }
1694
1695 /**
1696 * 创建视频附件。用于上传视频文件。
1697 *
1698 * @par 使用前提:
1699 * 已登录 IM。
1700 * @par 参数说明:
1701 * <table>
1702 * <tr>
1703 * <th>参数名称</th>
1704 * <th>描述</th>
1705 * </tr>
1706 * <tr>
1707 * <td>file</td>
1708 * <td>Android File 对象。</td>
1709 * </tr>
1710 * <tr>
1711 * <td>duration</td>
1712 * <td>视频文件时长,单位为毫秒。</td>
1713 * </tr>
1714 * <tr>
1715 * <td>width</td>
1716 * <td>视频宽度,单位为像素。</td>
1717 * </tr>
1718 * <tr>
1719 * <td>height</td>
1720 * <td>视频高度,单位为像素。</td>
1721 * </tr>
1722 * <tr>
1723 * <td>displayName</td>
1724 * <td>视频文件显示名称,可不同于文件名。</td>
1725 * </tr>
1726 * <tr>
1727 * <td>nosTokenSceneKey</td>
1728 * <td>视频文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1729 * </tr>
1730 * </table>
1731 * @return
1732 * VideoAttachment 对象。
1733 *
1734 */
1735 @androidx.annotation.NonNull
1736 public static VideoAttachment createVideoAttachment(File file, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
1737 final VideoAttachment attachment = new VideoAttachment();
1738 attachment.setPath(file.getPath());
1739 attachment.setSize(file.length());
1740 attachment.setDuration(duration);
1741 attachment.setWidth(width);
1742 attachment.setHeight(height);
1743 attachment.setDisplayName(displayName);
1744 attachment.setExtension(StringUtil.getExtension(file.getName()));
1745 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1746 return attachment;
1747 }
1748
1749 /**
1750 * 创建视频附件。用于上传视频文件。
1751 *
1752 * @par 使用前提:
1753 * 已登录 IM。
1754 * @par 参数说明:
1755 * <table>
1756 * <tr>
1757 * <th>参数名称</th>
1758 * <th>描述</th>
1759 * </tr>
1760 * <tr>
1761 * <td>context</td>
1762 * <td>Android Context 对象。</td>
1763 * </tr>
1764 * <tr>
1765 * <td>uri</td>
1766 * <td>视频文件 URI 地址。</td>
1767 * </tr>
1768 * <tr>
1769 * <td>duration</td>
1770 * <td>视频文件时长,单位为毫秒。</td>
1771 * </tr>
1772 * <tr>
1773 * <td>width</td>
1774 * <td>视频宽度,单位为像素。</td>
1775 * </tr>
1776 * <tr>
1777 * <td>height</td>
1778 * <td>视频高度,单位为像素。</td>
1779 * </tr>
1780 * <tr>
1781 * <td>displayName</td>
1782 * <td>视频文件显示名称,可不同于文件名。</td>
1783 * </tr>
1784 * <tr>
1785 * <td>nosTokenSceneKey</td>
1786 * <td>视频文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1787 * </tr>
1788 * </table>
1789 * @return
1790 * VideoAttachment 对象。
1791 *
1792 */
1793 @androidx.annotation.NonNull
1794 public static VideoAttachment createVideoAttachment(Context context, Uri uri, long duration, int width, int height, String displayName,
1795 String nosTokenSceneKey) {
1796 final VideoAttachment attachment = new VideoAttachment();
1797 attachment.setUri(uri);
1798 attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1799 attachment.setDuration(duration);
1800 attachment.setWidth(width);
1801 attachment.setHeight(height);
1802 attachment.setDisplayName(displayName);
1803 attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1804 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1805 return attachment;
1806 }
1807
1808 /**
1809 * 创建文件附件。用于上传文件。
1810 *
1811 * @par 使用前提:
1812 * 已登录 IM。
1813 * @par 参数说明:
1814 * <table>
1815 * <tr>
1816 * <th>参数名称</th>
1817 * <th>描述</th>
1818 * </tr>
1819 * <tr>
1820 * <td>file</td>
1821 * <td>Android File 对象。</td>
1822 * </tr>
1823 * <tr>
1824 * <td>displayName</td>
1825 * <td>文件显示名称,可不同于文件名。</td>
1826 * </tr>
1827 * <tr>
1828 * <td>nosTokenSceneKey</td>
1829 * <td>文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1830 * </tr>
1831 * </table>
1832 * @return
1833 * FileAttachment 对象。
1834 *
1835 */
1836 @androidx.annotation.NonNull
1837 public static FileAttachment createFileAttachment(File file, String displayName, String nosTokenSceneKey) {
1838 final FileAttachment attachment = new FileAttachment();
1839 attachment.setPath(file.getPath());
1840 attachment.setSize(file.length());
1841 attachment.setDisplayName(displayName);
1842 attachment.setExtension(StringUtil.getExtension(file.getName()));
1843 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1844 return attachment;
1845 }
1846
1847 /**
1848 * 创建文件附件。用于上传文件。
1849 *
1850 * @par 使用前提:
1851 * 已登录 IM。
1852 * @par 参数说明:
1853 * <table>
1854 * <tr>
1855 * <th>参数名称</th>
1856 * <th>描述</th>
1857 * </tr>
1858 * <tr>
1859 * <td>context</td>
1860 * <td>Android Context 对象。</td>
1861 * </tr>
1862 * <tr>
1863 * <td>uri</td>
1864 * <td>文件 URI 地址。</td>
1865 * </tr>
1866 * <tr>
1867 * <td>displayName</td>
1868 * <td>文件显示名称,可不同于文件名。</td>
1869 * </tr>
1870 * <tr>
1871 * <td>nosTokenSceneKey</td>
1872 * <td>文件上传时使用的 NOS Scene Key,详见 NimNosSceneKeyConstant。</td>
1873 * </tr>
1874 * </table>
1875 * @return
1876 * FileAttachment 对象。
1877 *
1878 */
1879 @androidx.annotation.NonNull
1880 public static FileAttachment createFileAttachment(Context context, Uri uri, String displayName, String nosTokenSceneKey) {
1881 final FileAttachment attachment = new FileAttachment();
1882 attachment.setUri(uri);
1883 attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1884 attachment.setDisplayName(displayName);
1885 attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1886 attachment.setNosTokenSceneKey(nosTokenSceneKey);
1887 return attachment;
1888 }
1889}
云信 IM 消息构造器,提供构建各类型消息的接口。
static IMMessage createFileMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName)
创建一条文件消息。
static IMMessage createForwardMessage(IMMessage message, String sessionId, SessionTypeEnum sessionType)
创建一条待转发消息。
static ImageAttachment createImageAttachment(File file, String displayName, String nosTokenSceneKey)
创建图片附件,用于上传图片文件。
static IMMessage createFromJson(String json)
创建一条 JSON 消息。
static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, MsgAttachment attachment)
创建一条自定义消息。
static IMMessage createAudioMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri, long duration)
创建一条语音消息。
static IMMessage createLocationMessage(String sessionId, SessionTypeEnum sessionType, double lat, double lng, String addr)
创建一条地理位置消息。
static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName)
创建一条图片消息。
static IMMessage createTextMessage(String sessionId, SessionTypeEnum sessionType, String text)
创建一条文本消息。
static IMMessage createTipMessage(String sessionId, SessionTypeEnum sessionType)
创建一条提醒消息。
static IMMessage createFileMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName, String nosTokenSceneKey)
创建一条文件消息。
static IMMessage createAudioMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, String nosTokenSceneKey)
创建一条语音消息。
static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName, String nosTokenSceneKey)
创建一条图片消息。
static IMMessage createAudioMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration)
创建一条语音消息。
static IMMessage createImageMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName, String nosTokenSceneKey)
创建一条图片消息。
static IMMessage createRobotMessage(String sessionId, SessionTypeEnum sessionType, String robotAccount, String text, String type, String content, String target, String params)
该方法不推荐使用,请使用 MessageBuilder#createRobotTextMessage 和 MessageBuilder#createRobotTipMessage。
static IMMessage createFileMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName)
创建一条文件消息。
static String createForwardMessageListFileDetail(List<? extends IMMessage > messages)
创建多条待合并转发的消息。
static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment, CustomMessageConfig config)
创建一条自定义消息。
static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment, CustomMessageConfig config, String nosTokenSceneKey)
创建一条自定义消息。
static IMMessage createNrtcNetcallMessage(String sessionId, SessionTypeEnum sessionType, MsgAttachment netCallAttachment)
创建一条通话记录消息。
static IMMessage createRobotTextMessage(String sessionId, SessionTypeEnum sessionType, String text, MessageRobotInfo messageRobotInfo)
创建一条机器人普通文本消息。
static IMMessage createVideoMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, int width, int height, String displayName)
创建一条视频消息。
static IMMessage createImageMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri)
创建一条图片消息。
static String convertMessageToJson(IMMessage msg)
将 IMMessage 对象转换成 JSON 格式的字符串。
static AudioAttachment createAudioAttachment(Context context, Uri uri, long duration, String nosTokenSceneKey)
创建音频附件。用于上传音频文件。
static IMMessage createVideoMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, int width, int height, String displayName, String nosTokenSceneKey)
创建一条视频消息。
static AudioAttachment createAudioAttachment(File file, long duration, String nosTokenSceneKey)
创建音频附件。用于上传音频文件。
static IMMessage createImageMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName)
创建一条图片消息。
static IMMessage createAudioMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, String nosTokenSceneKey)
创建一条语音消息。
static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file)
创建一条图片消息。
static ImageAttachment createImageAttachment(Context context, Uri uri, String displayName, String nosTokenSceneKey)
创建图片附件,用于上传图片文件。
static VideoAttachment createVideoAttachment(File file, long duration, int width, int height, String displayName, String nosTokenSceneKey)
创建视频附件。用于上传视频文件。
static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment)
创建一条自定义消息。
static FileAttachment createFileAttachment(File file, String displayName, String nosTokenSceneKey)
创建文件附件。用于上传文件。
static IMMessage createEmptyMessage(String sessionId, SessionTypeEnum sessionType, long time)
创建一条空消息。
static FileAttachment createFileAttachment(Context context, Uri uri, String displayName, String nosTokenSceneKey)
创建文件附件。用于上传文件。
static VideoAttachment createVideoAttachment(Context context, Uri uri, long duration, int width, int height, String displayName, String nosTokenSceneKey)
创建视频附件。用于上传视频文件。
static IMMessage createFileMessage(Context context, String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName, String nosTokenSceneKey)
创建一条文件消息。
static IMMessage createRobotTipMessage(String sessionId, SessionTypeEnum sessionType, MessageRobotInfo messageRobotInfo)
创建一条机器人提醒消息。
void setDuration(long duration)
设置音频的播放时长
带有文件的附件类型的基类 描述文件的相关信息
boolean setUri(Uri uri)
设置文件uri, 仅支持ContentResolver.SCHEME_FILE类型和ContentResolver.SCHEME_CONTENT类型的uri 仅支持发送消息时使用,接收的消息附件不支持u...
String getUrl()
获取文件在服务器上的下载url。若文件还未上传,返回null
String getThumbPathForSave()
获取用于保存缩略图文件的位置
void setSize(long size)
设置文件大小,单位为byte
void setExtension(String extension)
设置文件后缀名
void setDisplayName(String displayName)
设置文件显示名
void setNosTokenSceneKey(String nosTokenSceneKey)
设置文件上传时的nos scene
void setAddress(String address)
设置地理位置描述信息
void setDuration(long duration)
设置视频的播放时长
void setWidth(int width)
设置视频的宽度
void setHeight(int height)
设置视频的高度
消息的配置选项,主要用于设定消息的声明周期,是否需要推送,是否需要计入未读数等。
void initSend(String robotAccount, String type, String content, String target, String params)
static long getFileSizeFromUri(Context context, Uri uri)
从URI中获取文件的实际大小
Definition: UriUtils.java:190
static String getFileExtensionFromUri(Context context, Uri uri)
从URI中获取文件的扩展名,如果没有则返回空字符串
Definition: UriUtils.java:160
消息本身发送/接收状态,附件状态见AttachStatusEnum
NIM消息实体数据结构。 第三方APP不要调用设置类接口,调用之后不会被持久化
Definition: IMMessage.java:12
void setFromAccount(String account)
设置说话方的帐号。消息方向getDirect()根据改之
MsgTypeEnum getMsgType()
获取消息类型。