发送群 @ 消息
在群组会话中,IMLib 支持发送群组 @ 消息,可用于提醒群内所有成员或指定成员。
构造 @ 消息
待发送的消息内容可以是内置消息类型(如 RongIMLib.TextMessage)的实例,也可以是通过 RongIMLib.registerMessageType() 自定义注册的消息类型实例。
@ 消息 的构造必须设置 mentionedInfo 参数
参数说明
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
content | string | 是 | 文本内容 |
mentionedInfo | MentionedInfo | 否 | @ 消息的构造参数 |
- mentionedInfo 参数说明
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
type | number | 是 | @ 消息类型,0:@ 所有人,1:@ 指定用户 |
userIdList | string[] | 否 | @ 的用户 id 列表 |
mentionedContent | string | 否 | 推送通知内容,例如 '有人@你'。@消息携带的 mentionedContent 优先级最高,会覆盖所有默认或自定义的 pushContent 数据。 |
示例代码
JavaScript
const details = {
content: '我是一条文字信息',
mentionedInfo: {
type: 1, //1: @ 所有人 2: @ 指定用户
userIdList: ['user1'],//被 @ 的用户 Id 列表
mentionedContent: '有人@你'//携带扩展信息,例如 `有人@你`
}
}
const message = new RongIMLib.TextMessage(details)
发送 @ 消息
调用 sendMessage 在群组中发送 @ 消息。请务必在 options 中设置 isMentioned 为 true,该配置项仅在群组会话 (ConversationType.GROUP
) 中生效。
参数说明
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
conversation | IConversationOption | 是 | 目标会话 |
message | BaseMessage | 是 | 待发送的消息实例。类型为 BaseMessage 的子类,可为 IMLib 内置消息(如 RongIMLib.TextMessage)或通过 RongIMLib.registerMessageType() 自定义的消息实例 |
options | ISendMessageOptions | 否 | 发送配置项。定义发送行为中的一些可选配置,如是否可拓展,推送等 |
示例代码
JavaScript
const details = {
content: '我是一条文字信息',
mentionedInfo: {
type: 1,
userIdList: ['user1'],
mentionedContent: '有人@你'
}
}
const message = new RongIMLib.TextMessage(details)
//发送 @ 消息,只当 conversationType 值为 `ConversationType.GROUP` 时有效
const conversation = {
conversationType: RongIMLib.ConversationType.GROUP,
targetId: '目标ID'
}
const options = {
isMentioned: true
}
RongIMLib.sendMessage(conversation, message, options).then(res => {
if(res.code === 0){
// 发送群@消息成功
console.log(res.data)
} else {
console.log(res.code, res.msg);
}
})