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