发送消息
本文主要描述了如何使用 IMLib SDK 向单聊会话、群聊会话、聊天室会话中发送消息。
消息内容类型简介
IMLib SDK 定义的 Message 对象的 content
属性中可包含两大类消息内容:普通消息内容和媒体消息内容。普通消息内容父类是 MessageContent,媒体消息内容父类是 MediaMessageContent。发送媒体消息和普通消息本质的区别为是否有上传数据过程。
功能 | 消息内容的类型 | 父类 | 描述 |
---|---|---|---|
文本消息 | TextMessage | MessageContent | 文本消息的内容。 |
图片消息 | ImageMessage | MediaMessageContent | 图片消息的内容,支持发送原图。 |
文件消息 | FileMessage | MediaMessageContent | 文件消息的内容。 |
语音消息 | HQVoiceMessage | MediaMessageContent | 高清语音消息的内容。 |
提及他人(@ 消息) | 不适用 | 不适用 | @消息并非预定义的消息类型。详见如何发送 @ 消息。 |
以上为 IMLib SDK 内置的部分消息内容类型。您还可以创建自定义的消息内容类型,并使用 sendMessage
方法或 sendMediaMessage
方法发送。详见自定义消息类型。
- 发送普通消息请使用
sendMessage
方法,发送媒体消息请使用sendMediaMessage
方法。 - 客户端 SDK 发送消息存在频率限制,每秒最多只能发送 5 条消息。
本文使用 IMLib SDK 核心类 IMEngine
的发送消息方法。
普通消息
普通消息指文本消息等不涉及媒体文件上传的消息。普通消息的消息内容为 MessageContent
的子类的消息,例如文本消息内容(TextMessage),或自定义类型的普通消息内容。
构造普通消息
您在发送普通消息前,需要先构造 Message 对象。conversationType
字段为会话类型,targetId
表示会话的目标 ID。以下示例中构造了一条包含文本消息内容(TextMessage)的消息对象。
构造文本消息示例代码
let conId = new ConversationIdentifier();
conId.conversationType = ConversationType.Private;
conId.targetId = "会话 id";
let textMsg = new TextMessage();
textMsg.content = "文本消息的内容";
let msg = new Message(conId, textMsg);
构造命令消息示例代码
- 1.2.0 版本开始支持 CommandMessage 消息。
- CommandMessage 不存储不计数,通常由 App 服务发送,不会在 UI 上展示。
let cmdMsg = new CommandMessage();
cmdMsg.name = "命名名称";
cmdMsg.data = "命令数据,可以放 json 串";
构造命令提醒消息示例代码
- 1.2.0 版本开始支持 CommandNotificationMessage 消息。
- CommandNotificationMessage 命令提醒消息类,一般在 UI 上当做小灰条消息展示。
let cmdMsg = new CommandNotificationMessage();
cmdMsg.name = "命名名称";
cmdMsg.data = "命令数据,可以放 json 串";
构造引用消息示例代码
- 1.2.0 版本开始支持 ReferenceMessage 消息。
// 此处的 message 对象必须是发送成功的消息或者是接收的消息,此处进行简写
let message : Message ;
let referMsg = new ReferenceMessage();
referMsg.content = "发送的文本内容";
referMsg.referMsgUserId = message.senderId; // 被引用消息的发送者 ID
referMsg.objName = message.objectName; // 被引用的消息 objectName
referMsg.referMsg = message.content; // 被引用的消息体
referMsg.referMsgUid = message.messageUid; // 被引用消息的 messageUId
构造图文消息示例代码
- 1.2.0 版本开始支持 RichContentMessage 消息。
let richMsg = new RichContentMessage();
richMsg.title = "图文消息的标题";
richMsg.content = "图文消息的内容摘要";
richMsg.imgUrl = "图文消息图片 URL";
richMsg.url = "图文消息中包含的需要跳转到的 URL";
发送普通消息
如果 Message 对象包含普通消息内容,您可以使用 IMLib SDK 核心类 IMEngine
的 sendMessage*
方法发送消息。
接口原型
public sendMessage(msg: Message): Promise<IAsyncResult<Message>>;
sendMessage
方法使用消息推送属性配置 PushConfig
,其中包含了 pushContent
和 pushData
。详见远程推送通知。
关于是否需要配置输入参数或 Message
的 pushConfig
属性中 pushContent
,请参考以下内容:
- 如果消息类型为即时通讯服务预定义消息类型中的用户内容类消息格式,例如 TextMessage,
pushContent
可设置为null
。一旦消息触发离线推送通知时,远程推送通知默认使用服务端预置的推送 通知内容。关于各类型消息的默认推送通知内容,详见用户内容类消息格式。 - 如果消息类型为即时通讯服务预定义消息类型中通知类、信令类("撤回命令消息" 除外),且需要支持远程推送通知,则必须填写
pushContent
,否则收件人不在线时无法收到远程推送通知。如无需触发远程推送,可不填该字段。 - 如果消息类型为自定义消息类型,请参考自定义消息如何支持远程推送。
- 请注意,聊天室会话不支持离线消息机制,因此也不支持离线消息转推送。
参数说明
参数 | 类型 | 说明 |
---|---|---|
message | Message | 要发送的消息体。必填属性包括会话类型(conversationType),会话 ID(targetId ),消息内容(content )。详见消息介绍 中对 Message 的结构说明。 |
option | ISendMsgOption | 发送消息配置,目前为空 |
通过 IMEngine 的 sendMessage 方法的,融云服务器始终会通知您的消息是否已发送成功。当因任何问题导致发送失败时,可通过回调方法返回异常。
示例代码
let option: ISendMsgOption = {};
IMEngine.getInstance()
.sendMessage(msg, option, (msg: Message) => {
// 消息入库
})
.then(result => {
if (EngineError.Success !== result.code) {
// 发送消息失败
return;
}
if (!result.data) {
// 消息数据为空
return;
}
let msg = result.data as Message;
})
- 关于如何个性化配置接收方离线时收到的远程推送通知,详见下文远程推送通知。
- 自定义消息类型默认不支持离线消息转推送机制。如需支持,详见下文自定义消息如何支持远程推送。
发送定向普通消息
1.3.0 版本开始支持该功能,仅群组支持该功能。
群组发送消息默认发送给群组中所有用户,如果需要将消息发给群组的部分用户,您可以使用 sendDirectionalMessage
方法。
发送定向普通消息和发送普通消息只是多了需要定向的用户 id 数组,其余和发送普通消息一致。
示例代码
let option: ISendMsgOption = {};
// 填入实际的用户 id
let userIdArray = ["userId1", "userId2"];
IMEngine.getInstance().sendDirectionalMessage(msg, option, userIdArray, (msg: Message) => {
// 消息入库回调
}).then(result => {
if (EngineError.Success !== result.code) {
// 发送消息失败
return;
}
if (!result.data) {
// 消息数据为空
return;
}
let msg = result.data as Message;
})
媒体消息
媒体消息的发送参考: 发送媒体消息
如何发送 @ 消息
@消息在融云即时通讯服务中不属于一种预定义的消息类型(详见服务端文档 消息类型概述)。融云通过在消息中携带 MentionedInfo
数据,帮助 App 实现提及他人(@)功能。
消息的 MessageContent 中的 MentionedInfo 字段存储了携带所 @ 人员的信息。无论是 SDK 内置消息类型,或者您自定义的消息类型,都直接或间接继承了 MessageContent 类。
融云支持在向群组和超级群中发消息时,在消息内容中添加 MentionedInfo。消息发送前,您可以构造 MentionedInfo,并设置到消息的 MessageContent
中。
构造 @ 所有人消息
// @ 所有人
let textMsg = new TextMessage();
textMsg.content = "from 鸿蒙 ";
textMsg.extra = "TestExtra";
// 消息 @ 所有人
let mentionedInfo = new MentionedInfo();
// @ 类型为 @ 所有人
mentionedInfo.type = MentionedType.ALL;
// 把 @ 信息塞给消息体
textMsg.mentionedInfo = mentionedInfo;
构造 @ 部分用户消息
// @ 部分用户
let textMsg = new TextMessage();
textMsg.content = "from 鸿蒙 ";
textMsg.extra = "TestExtra";
// 消息的 @ 信息
let mentionedInfo = new MentionedInfo();
// @ 类型为 @ 部分用户
mentionedInfo.type = MentionedType.PART;
// 保存 @ 的用户 id 列表
let userIdList = new Array<string>();
userIdList.push("TestUserId");
mentionedInfo.userIdList = userIdList;
// 把 @ 信息塞给消息体
textMsg.mentionedInfo = mentionedInfo;