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