NIMSDK-AOS  10.5.0
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 
7 import com.netease.nimlib.BuildConfig;
8 import com.netease.nimlib.NimNosSceneKeyConstant;
9 import com.netease.nimlib.SDKCache;
10 import com.netease.nimlib.SDKCacheUI;
11 import com.netease.nimlib.biz.constant.IAuthService;
12 import com.netease.nimlib.migration.MessageConvert;
13 import com.netease.nimlib.notifier.support26.annotation.NonNull;
31 import com.netease.nimlib.session.IMMessageImpl;
32 import com.netease.nimlib.util.BitmapDecoder;
33 import com.netease.nimlib.util.StringUtil;
34 import com.netease.nimlib.util.TimeUtil;
35 
36 import org.json.JSONException;
37 import org.json.JSONObject;
38 
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.List;
42 
46 public class MessageBuilder {
47 
76  public static IMMessage createTextMessage(String sessionId, SessionTypeEnum sessionType, String text) {
77  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
78  msg.setMsgType(MsgTypeEnum.text.getValue());
79  msg.setContent(text);
80  return msg;
81  }
82 
111  public static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file) {
112  return createImageMessage(sessionId, sessionType, file, null, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
113  }
114 
147  public static IMMessage createImageMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri) {
148  return createImageMessage(context,sessionId, sessionType, uri, null, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
149  }
150 
183  public static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName) {
184  return createImageMessage(sessionId, sessionType, file, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
185  }
186 
223  public static IMMessage createImageMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri,String displayName) {
224  return createImageMessage(context,sessionId, sessionType, uri, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
225  }
226 
263  public static IMMessage createImageMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName, String nosTokenSceneKey) {
264  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
265  msg.setMsgType(MsgTypeEnum.image.getValue());
266 
267  final ImageAttachment attachment = createImageAttachment(file, displayName, nosTokenSceneKey);
268  msg.setAttachment(attachment);
269  return msg;
270  }
271 
312  public static IMMessage createImageMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName, String nosTokenSceneKey) {
313  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
314  msg.setMsgType(MsgTypeEnum.image.getValue());
315  final ImageAttachment attachment = createImageAttachment(context, uri, displayName, nosTokenSceneKey);
316  msg.setAttachment(attachment);
317  return msg;
318  }
319 
352  public static IMMessage createAudioMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration) {
353  return createAudioMessage(sessionId, sessionType, file, duration, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
354  }
355 
392  public static IMMessage createAudioMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration) {
393  return createAudioMessage(context,sessionId, sessionType, uri, duration, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
394  }
395 
432  public static IMMessage createAudioMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, String nosTokenSceneKey) {
433  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
434  msg.setMsgType(MsgTypeEnum.audio.getValue());
435  if (duration > 0 && duration < 1000) {// 最低显示1秒
436  duration = 1000;
437  }
438  final AudioAttachment attachment = createAudioAttachment(file, duration, nosTokenSceneKey);
439  msg.setAttachment(attachment);
440  return msg;
441  }
442 
483  public static IMMessage createAudioMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, String nosTokenSceneKey) {
484  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
485  msg.setMsgType(MsgTypeEnum.audio.getValue());
486  if (duration > 0 && duration < 1000) {// 最低显示1秒
487  duration = 1000;
488  }
489  final AudioAttachment attachment = createAudioAttachment(context, uri, duration, nosTokenSceneKey);
490  msg.setAttachment(attachment);
491  return msg;
492  }
493 
530  public static IMMessage createLocationMessage(String sessionId, SessionTypeEnum sessionType, double lat, double lng, String addr) {
531  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
532  msg.setMsgType(MsgTypeEnum.location.getValue());
533 
534  final LocationAttachment location = new LocationAttachment();
535  location.setLatitude(lat);
536  location.setLongitude(lng);
537  location.setAddress(addr);
538  msg.setAttachStatus(AttachStatusEnum.transferred);
539  msg.setAttachment(location);
540 
541  return msg;
542  }
543 
588  public static IMMessage createVideoMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, int width, int height, String displayName) {
589  return createVideoMessage(sessionId, sessionType, file, duration, width, height, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
590  }
591 
640  @SuppressWarnings("java:S107")
641  public static IMMessage createVideoMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, int width, int height, String displayName) {
642  return createVideoMessage(context,sessionId, sessionType, uri, duration, width, height, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
643  }
644 
693  public static IMMessage createVideoMessage(String sessionId, SessionTypeEnum sessionType, File file, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
694  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
695  msg.setMsgType(MsgTypeEnum.video.getValue());
696 
697  final VideoAttachment attachment = createVideoAttachment(file, duration, width, height, displayName, nosTokenSceneKey);
698  msg.setAttachment(attachment);
699 
700  BitmapDecoder.extractThumbnail(file.getPath(), attachment.getThumbPathForSave());
701 
702  return msg;
703  }
704 
757  @SuppressWarnings("java:S107")
758  public static IMMessage createVideoMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
759  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
760  msg.setMsgType(MsgTypeEnum.video.getValue());
761  final VideoAttachment attachment = createVideoAttachment(context, uri, duration, width, height, displayName, nosTokenSceneKey);
762  msg.setAttachment(attachment);
763 
764  BitmapDecoder.extractThumbnail(uri, attachment.getThumbPathForSave());
765 
766  return msg;
767  }
768 
801  public static IMMessage createFileMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName) {
802  return createFileMessage(sessionId, sessionType, file, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
803  }
804 
837  public static IMMessage createFileMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName) {
838  return createFileMessage(context,sessionId, sessionType, uri, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
839  }
840 
877  public static IMMessage createFileMessage(String sessionId, SessionTypeEnum sessionType, File file, String displayName, String nosTokenSceneKey) {
878  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
879  msg.setMsgType(MsgTypeEnum.file.getValue());
880 
881  final FileAttachment attachment = createFileAttachment(file, displayName, nosTokenSceneKey);
882  msg.setAttachment(attachment);
883  return msg;
884  }
885 
926  public static IMMessage createFileMessage(Context context,String sessionId, SessionTypeEnum sessionType, Uri uri, String displayName, String nosTokenSceneKey) {
927  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
928  msg.setMsgType(MsgTypeEnum.file.getValue());
929  final FileAttachment attachment = createFileAttachment(context, uri, displayName, nosTokenSceneKey);
930  msg.setAttachment(attachment);
931  return msg;
932  }
933 
958  public static IMMessage createTipMessage(String sessionId, SessionTypeEnum sessionType) {
959  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
960  msg.setMsgType(MsgTypeEnum.tip.getValue());
961 
962  return msg;
963  }
964 
997  public static IMMessage createRobotTextMessage(String sessionId, SessionTypeEnum sessionType, String text, MessageRobotInfo messageRobotInfo) {
998  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
999  msg.setMsgType(MsgTypeEnum.text.getValue());
1000  msg.setContent(text);
1001  msg.setRobotInfo(messageRobotInfo);
1002  return msg;
1003  }
1004 
1033  public static IMMessage createRobotTipMessage(String sessionId, SessionTypeEnum sessionType, MessageRobotInfo messageRobotInfo) {
1034  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1035  msg.setMsgType(MsgTypeEnum.tip.getValue());
1036  msg.setRobotInfo(messageRobotInfo);
1037  return msg;
1038  }
1039 
1070  public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, MsgAttachment attachment) {
1071  return createCustomMessage(sessionId, sessionType, null, attachment, null);
1072  }
1073 
1108  public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment) {
1109  return createCustomMessage(sessionId, sessionType, content, attachment, null);
1110  }
1111 
1150  public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment, CustomMessageConfig config) {
1151  return createCustomMessage(sessionId, sessionType, content, attachment, config, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
1152  }
1153 
1196  public static IMMessage createCustomMessage(String sessionId, SessionTypeEnum sessionType, String content, MsgAttachment attachment, CustomMessageConfig config, String nosTokenSceneKey) {
1197  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1198  msg.setMsgType(MsgTypeEnum.custom.getValue());
1199  msg.setContent(content);
1200  if (attachment != null && attachment instanceof FileAttachment) {
1201  ((FileAttachment) attachment).setNosTokenSceneKey(nosTokenSceneKey);
1202  }
1203  msg.setAttachment(attachment);
1204  msg.setConfig(config);
1205  return msg;
1206  }
1207 
1211  public static IMMessage createRobotMessage(String sessionId, SessionTypeEnum sessionType, String robotAccount, String text, String type, String content, String target, String params) {
1212  if (TextUtils.isEmpty(type) || TextUtils.isEmpty(robotAccount)) {
1213  throw new IllegalArgumentException("Invalid param, type and robot account should not be null");
1214  }
1215 
1216  if (type.equals(RobotMsgType.TEXT) && content == null) {
1217  throw new IllegalArgumentException("Invalid param, content should not be null");
1218  } else if (type.equals(RobotMsgType.LINK) && TextUtils.isEmpty(target)) {
1219  throw new IllegalArgumentException("Invalid param, target should not be null");
1220  }
1221 
1222  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1223  msg.setMsgType(MsgTypeEnum.robot.getValue());
1224 
1225  // build attachment
1226  RobotAttachment robotAttachment = new RobotAttachment();
1227  robotAttachment.initSend(robotAccount, type, content, target, params);
1228  msg.setAttachment(robotAttachment);
1229  msg.setContent(text); // 文案显示用,便于全文检索等
1230 
1231  return msg;
1232  }
1233 
1262  public static IMMessage createNrtcNetcallMessage(String sessionId, SessionTypeEnum sessionType, MsgAttachment netCallAttachment) {
1263  IMMessageImpl msg = initSendMessage(sessionId, sessionType);
1264  msg.setMsgType(MsgTypeEnum.nrtc_netcall.getValue());
1265  msg.setAttachment(netCallAttachment);
1266  return msg;
1267  }
1268 
1299  public static IMMessage createEmptyMessage(String sessionId, SessionTypeEnum sessionType, long time) {
1300  IMMessageImpl msg = new IMMessageImpl();
1301  msg.setSessionId(sessionId);
1302  msg.setSessionType(sessionType);
1303  msg.setTime(time);
1304  return msg;
1305  }
1306 
1339  public static IMMessage createForwardMessage(IMMessage message, String sessionId, SessionTypeEnum sessionType) {
1340  if (message.getMsgType() == MsgTypeEnum.notification || message.getMsgType() == MsgTypeEnum.avchat || message.getMsgType() == MsgTypeEnum.robot) {
1341  return null;
1342  }
1343  IMMessageImpl forward = ((IMMessageImpl) message).deepClone();
1344  if (forward != null) {
1345  forward.setSessionId(sessionId);
1346  forward.setSessionType(sessionType);
1347  forward.setUuid(StringUtil.get32UUID());
1348  forward.setFromAccount(SDKCacheUI.getAccount());
1349  forward.setDirect(MsgDirectionEnum.Out);
1350  forward.setStatus(MsgStatusEnum.sending);
1351  forward.setTime(TimeUtil.currentTimeMillis());
1352  forward.setServerId(0L);
1353  forward.setMessageId(0L);
1354  // clear team msg ack
1355  forward.setMsgAck(false);
1356  forward.setHasSendAck(false);
1357  forward.setTeamMsgAckCount(0);
1358  forward.setTeamMsgUnAckCount(0);
1359 
1360  // 接收到消息,但是附件没有下载成功的情况。不会再次上传附件,所以要将附件的发送状态直接改为def
1361  final MsgAttachment attachment = forward.getAttachment();
1362  if (attachment != null && attachment instanceof FileAttachment) {
1363  final FileAttachment media = (FileAttachment) attachment;
1364  if (!TextUtils.isEmpty(media.getUrl())) {
1365  forward.setAttachStatus(AttachStatusEnum.def);
1366  }
1367  }
1368  }
1369 
1370  return forward;
1371  }
1372 
1373  private static IMMessageImpl initSendMessage(String toId, SessionTypeEnum sessionType) {
1374  IMMessageImpl msg = new IMMessageImpl();
1375 
1376  msg.setUuid(StringUtil.get32UUID());
1377  msg.setSessionId(toId);
1378  msg.setFromAccount(SDKCacheUI.getAccount());
1379  msg.setDirect(MsgDirectionEnum.Out);
1380  msg.setStatus(MsgStatusEnum.sending);
1381  msg.setSessionType(sessionType);
1382  msg.setTime(TimeUtil.currentTimeMillis());
1383 
1384  return msg;
1385  }
1386 
1407  public static String createForwardMessageListFileDetail(List<? extends IMMessage> messages) {
1408  if (messages == null || messages.isEmpty()) {
1409  return "";
1410  }
1411  final String sessionId = messages.get(0).getSessionId();
1412  if (sessionId==null) {
1413  return "";
1414  }
1415  List<IMMessage> msgList = new ArrayList<>(messages.size());
1416  for (IMMessage msg : messages) {
1417  // 不是来自同一会话,不添加
1418  if (!sessionId.equals(msg.getSessionId())){
1419  continue;
1420  }
1421  // 不是可以转发的类型,不添加
1422  if (msg.getMsgType() == MsgTypeEnum.notification || msg.getMsgType() == MsgTypeEnum.avchat || msg.getMsgType() == MsgTypeEnum.robot) {
1423  continue;
1424  }
1425  msgList.add(msg);
1426  }
1427  return buildHeader(0, msgList.size()) + "\n" + buildBody(msgList);
1428  }
1429 
1437  private static String buildHeader(int version, int message_count) {
1438  JSONObject obj = new JSONObject();
1439  try {
1440  obj.put("version", version);
1441  //终端类型:Android
1442  obj.put("terminal", IAuthService.ClientType.Android);
1443  obj.put("sdk_version", BuildConfig.VERSION_NAME);
1444  obj.put("app_version", BuildConfig.VERSION_CODE);
1445  obj.put("message_count", message_count);
1446  } catch (JSONException e) {
1447  e.printStackTrace();
1448  }
1449  return obj.toString();
1450  }
1451 
1458  private static String buildBody(@NonNull List<IMMessage> msgList) {
1459  String enter = "\n";
1460  if (msgList.isEmpty()) {
1461  return "";
1462  }
1463  StringBuilder stringBuilder = new StringBuilder();
1464  for (IMMessage msg : msgList) {
1465  stringBuilder.append(enter).append(MessageConvert.convertMessageToJson(msg, SDKCacheUI.getAccount()));
1466  }
1467  return stringBuilder.substring(enter.length());
1468  }
1469 
1470 
1491  public static IMMessage createFromJson(String json){
1492  if (TextUtils.isEmpty(json)) {
1493  return null;
1494  }
1495  return MessageConvert.convertJsonToMessage(json, SDKCacheUI.getAccount());
1496  }
1497 
1518  public static String convertMessageToJson(IMMessage msg) {
1519  if (msg == null) {
1520  return null;
1521  }
1522  return MessageConvert.convertMessageToJson(msg, SDKCacheUI.getAccount());
1523  }
1524 
1553  @androidx.annotation.NonNull
1554  public static ImageAttachment createImageAttachment(File file, String displayName, String nosTokenSceneKey) {
1555  final ImageAttachment attachment = new ImageAttachment();
1556  attachment.setPath(file.getPath());
1557  attachment.setSize(file.length());
1558  int[] dimension = BitmapDecoder.decodeBound(file);
1559  attachment.setWidth(dimension[0]);
1560  attachment.setHeight(dimension[1]);
1561  attachment.setDisplayName(displayName);
1562  attachment.setExtension(StringUtil.getExtension(file.getName()));
1563  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1564  return attachment;
1565  }
1566 
1599  @androidx.annotation.NonNull
1600  public static ImageAttachment createImageAttachment(Context context, Uri uri, String displayName, String nosTokenSceneKey) {
1601  final ImageAttachment attachment = new ImageAttachment();
1602  attachment.setUri(uri);
1603  attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1604  int[] dimension = BitmapDecoder.decodeBound(context, uri);
1605  attachment.setWidth(dimension[0]);
1606  attachment.setHeight(dimension[1]);
1607  attachment.setDisplayName(displayName);
1608  attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1609  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1610  return attachment;
1611  }
1612 
1641  @androidx.annotation.NonNull
1642  public static AudioAttachment createAudioAttachment(File file, long duration, String nosTokenSceneKey) {
1643  final AudioAttachment attachment = new AudioAttachment();
1644  attachment.setPath(file.getPath());
1645  attachment.setSize(file.length());
1646  attachment.setDuration(duration);// ms
1647  attachment.setExtension(StringUtil.getExtension(file.getName()));
1648  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1649  return attachment;
1650  }
1651 
1684  @androidx.annotation.NonNull
1685  public static AudioAttachment createAudioAttachment(Context context, Uri uri, long duration, String nosTokenSceneKey) {
1686  final AudioAttachment attachment = new AudioAttachment();
1687  attachment.setUri(uri);
1688  attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1689  attachment.setDuration(duration);// ms
1690  attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1691  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1692  return attachment;
1693  }
1694 
1735  @androidx.annotation.NonNull
1736  public static VideoAttachment createVideoAttachment(File file, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
1737  final VideoAttachment attachment = new VideoAttachment();
1738  attachment.setPath(file.getPath());
1739  attachment.setSize(file.length());
1740  attachment.setDuration(duration);
1741  attachment.setWidth(width);
1742  attachment.setHeight(height);
1743  attachment.setDisplayName(displayName);
1744  attachment.setExtension(StringUtil.getExtension(file.getName()));
1745  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1746  return attachment;
1747  }
1748 
1793  @androidx.annotation.NonNull
1794  public static VideoAttachment createVideoAttachment(Context context, Uri uri, long duration, int width, int height, String displayName,
1795  String nosTokenSceneKey) {
1796  final VideoAttachment attachment = new VideoAttachment();
1797  attachment.setUri(uri);
1798  attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1799  attachment.setDuration(duration);
1800  attachment.setWidth(width);
1801  attachment.setHeight(height);
1802  attachment.setDisplayName(displayName);
1803  attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1804  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1805  return attachment;
1806  }
1807 
1836  @androidx.annotation.NonNull
1837  public static FileAttachment createFileAttachment(File file, String displayName, String nosTokenSceneKey) {
1838  final FileAttachment attachment = new FileAttachment();
1839  attachment.setPath(file.getPath());
1840  attachment.setSize(file.length());
1841  attachment.setDisplayName(displayName);
1842  attachment.setExtension(StringUtil.getExtension(file.getName()));
1843  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1844  return attachment;
1845  }
1846 
1879  @androidx.annotation.NonNull
1880  public static FileAttachment createFileAttachment(Context context, Uri uri, String displayName, String nosTokenSceneKey) {
1881  final FileAttachment attachment = new FileAttachment();
1882  attachment.setUri(uri);
1883  attachment.setSize(UriUtils.getFileSizeFromUri(context, uri));
1884  attachment.setDisplayName(displayName);
1885  attachment.setExtension(UriUtils.getFileExtensionFromUri(context, uri));
1886  attachment.setNosTokenSceneKey(nosTokenSceneKey);
1887  return attachment;
1888  }
1889 }
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 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:12
消息本身发送/接收状态,附件状态见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()根据改之
static String createForwardMessageListFileDetail(List<? extends IMMessage > messages)
创建多条待合并转发的消息。
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)
创建一条自定义消息。