Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface MessageInterface

收发消息相关接口定义

Hierarchy

  • MessageInterface

Implemented by

Index

Methods

  • deleteMsg(options: { apnsText?: string; attach?: string; env?: string; msg: NIMMessage; ps?: string; pushPayload?: string; done?: any }): void
  • deprecated

    该方法已废弃,请使用 recallMsg

    Parameters

    • options: { apnsText?: string; attach?: string; env?: string; msg: NIMMessage; ps?: string; pushPayload?: string; done?: any }
      • Optional apnsText?: string

        自定义推送文案

      • Optional attach?: string

        附加信息,推荐使用 JSON 格式化字符串

      • Optional env?: string

        指定当前消息需要抄送到或者回调到的环境的名称。开通消息抄送或第三方回调后,该环境名称可在云信控制台中配置(如下图),最大 32 个字符。

        note

      • msg: NIMMessage

        待撤回的消息

      • Optional ps?: string

        附言

      • Optional pushPayload?: string

        第三方自定义的推送属性,限制使用 JSON 格式化的字符串。更多说明及相关示例请参见推送payload配置

      • done?:function

    Returns void

  • deleteMsgSelf(options: { custom?: string; msg: NIMMessage; done?: any }): void
  • 单向删除一条消息。删除后,消息对于其它用户仍可见。多端登录时,该函数会触发其它端 onDeleteMsgSelf 的回调

    Parameters

    Returns void

  • deleteMsgSelfBatch(options: { custom?: string; msgs: NIMMessage[]; done?: any }): void
  • 批量单向删除一条消息。删除后,消息对于其它用户仍可见。多端登录时,该函数会触发其它端 onDeleteMsgSelf 的回调

    示例场景

    Parameters

    Returns void

  • forwardMsg(options: { msg: NIMMessage; scene: "p2p" | "team" | "superTeam"; to: string; done?: any }): NIMMessage
  • getTeamMsgReadAccounts(options: { teamMsgReceipt: { idClient?: string; idServer: string; teamId: string }; done?: any }): void
  • getTeamMsgReads(options: { teamMsgReceipts: { idClient?: string; idServer: string; teamId: string }[]; done?: any }): void
  • 查询群组消息的已读、未读数量。

    • 对应于群组消息发送时配置了needMsgReceipt字段的群组消息,接收方可以对消息发送已读回执
    • done 回调的第二个参数是发送的参数,用于校验,第三个参数才是实际的结果
    • 在支持db,且存在idClient时:
      • 查询完成后,db中对应的msg会拥有 read和unread属性,分别对应消息的已读未读人数
      • unread为 0 后,消息的已读、未读数量便不会再变化了,因此建议在调用该接口前,过滤掉unread已为0的消息,减少不必要的性能消耗

    示例场景

    Parameters

    • options: { teamMsgReceipts: { idClient?: string; idServer: string; teamId: string }[]; done?: any }
      • teamMsgReceipts: { idClient?: string; idServer: string; teamId: string }[]

        待查询的群消息

      • done?:function
        • 注意这个回调的真实返回数据在第三个参数

          Parameters

          Returns void

    Returns void

  • isMsgRemoteRead(options: { msg: NIMMessage }): boolean
  • deprecated

    此接口用于查询p2p消息是否已接收已读回执,目前已废弃

    查询已读回执

    如何判断消息是否接收已读回执?当 msg.scene === 'p2p' && msg.flow === 'out' && msg.time <= session.msgReceiptTime 时,即可认为消息已收到已读回执

    Parameters

    Returns boolean

  • markMsgRead(options: { msgs: NIMMessage[]; done?: any }): void
  • recallMsg(options: { apnsText?: string; attach?: string; env?: string; msg: NIMMessage; ps?: string; pushPayload?: string; done?: any }): void
  • 描述

    撤回消息。

    使用场景

    消息发送后的可撤回时长(默认 2 分钟,可在云信控制台配置)内,发送方撤回已发送的单聊消息或者群消息。

    • 单聊场景下,发送方撤回之后,对应的离线消息、漫游消息和历史消息将被删除,消息接收方会收到一条类型为 deleteMsg 的系统通知。
    • 群聊场景下, 所有群成员都会收到类型为 deleteMsg 的系统通知。
    • 如果发送方同时在多个端登录了同一个 IM 账号, 那么其它端也会收到类型为 deleteMsg 的系统通知。

    注意

    单聊和群聊消息的撤回功能存在些许区别:

    • 单聊:用户只能撤回自己发送的消息。
    • 群聊:普通成员只能撤回自己发送的消息。管理员可撤回其他群成员的消息。

    使用限制

    • 如果消息发送失败或者消息发送者被拉黑,那么即使在可撤回时长内也无法撤回。
    • 消息撤回后,无论是发送方还是接收方,都无法在历史消息、漫游消息或者离线消息的接口返回中,查到该消息。
    • 以下情况下,消息撤回会失败:消息为空、消息没有发送成功、消息超过撤回时限,或者消息被反垃圾(内容审核)命中时。

    关联函数

    示例场景

    Parameters

    • options: { apnsText?: string; attach?: string; env?: string; msg: NIMMessage; ps?: string; pushPayload?: string; done?: any }
      • Optional apnsText?: string

        自定义推送文案

      • Optional attach?: string

        附加信息,推荐使用 JSON 格式化字符串

      • Optional env?: string

        指定当前消息需要抄送到或者回调到的环境的名称。开通消息抄送或第三方回调后,该环境名称可在云信控制台中配置(如下图),最大 32 个字符。

        note

      • msg: NIMMessage

        待撤回的消息

      • Optional ps?: string

        附言

      • Optional pushPayload?: string

        第三方自定义的推送属性,限制使用 JSON 格式化的字符串。更多说明及相关示例请参见推送payload配置

      • done?:function

    Returns void

  • 描述

    当 SDK 提供的能力无法满足您的业务需求时,调用该 API 进行个性化定制并在单聊和群聊场景(包括群组和超大群)中发送,例如石头剪刀布和投骰子功能。

    注意

    该接口直接返回的是 sending 状态的未完成消息体,发送完毕的消息体需要传递 options.done 获得。

    影响范围

    调用该 API 可触发:

    1. 接收方的 NIMGetInstanceOptions.onmsg 回调函数
    2. 接收方的 NIMGetInstanceOptions.onupdatesessions 回调
    3. 发送方的 NIMGetInstanceOptions.onupdatesessions 回调
    4. 发送方同时在线的其它客户端 NIMGetInstanceOptions.onmsg 回调

    示例场景

    示例

    nim.sendCustomMsg({
    scene: 'p2p',
    to: 'account',
    //接收方通过onMsg接收消息
    //然后如果msg.type === 'custom',接收方通过读取msg.content,然后调用业务代码
    content: JSON.stringify({type: 1}),
    done: function(err, msg) {
    if (err) {
    console.log('发送失败', err)
    } else {
    console.log('发送消息成功,消息为: ', msg)
    }
    }
    })

    Parameters

    Returns NIMMessage

  • 描述

    发送图片、视频、音频或其他文件。用户可以选择调用 sendFile 上传并发送文件。 也可以选择先调用 previewFile 上传文件,再调用 sendFile 发送文件

    注意一

    • 自版本 v8.9.102 和 v9.7.0+ 开始,该接口直接返回的是 sending 状态的未完成消息体,发送完毕的消息体需要传递 options.done 获得。

    注意二

    • fileInput、file、blob、filePath四个参数选择一个传入
    • fileInput: type='file'类型的input DOM元素的id。上传完成前请不要操作此节点上的文件
    • file: previewFile 回调函数的参数
    • blob: Blob类型JS对象
    • filePath: RN,小程序等特殊的 JS 运行环境专用(chooseImage 拿到的该临时路径)

    注意三

    • type: image、audio、video 或 file。默认为 file。主要区别在于消息体中 file 对象含有的信息不同
    • image: url, name, size, ext, w, h, type
    • audio: url, name, size, ext, container, dur
    • video: url, name, size, ext, container, dur, w, h
    • file: url, name, size, ext

    影响范围

    调用该 API 可触发:

    1. 接收方的 NIMGetInstanceOptions.onmsg 回调函数
    2. 接收方的 NIMGetInstanceOptions.onupdatesessions 回调
    3. 发送方的 NIMGetInstanceOptions.onupdatesessions 回调
    4. 发送方同时在线的其它客户端 NIMGetInstanceOptions.onmsg 回调

    直接发送文件 & 重发文件

    nim.sendFile({
    scene: 'p2p',
    to: 'account',
    type: 'image',
    fileInput: 'domId',
    done: function(err, obj) {
    if (err) {
    console.log('发送失败', err)
    // 重发. 当上传文件失败时 obj 参数的 msg 附带消息体,其他情况的 obj 就是消息体。
    setTimeout(function () {
    resendMessage(obj.msg ? obj.msg : obj)
    }, 3000)
    } else {
    console.log('发送消息成功,消息为: ', obj)
    }
    }
    })

    // 上传前已经能得到 idClient 做渲染
    console.log(message.idClient)

    // 重发
    function resendMessage(oldMessage) {
    nim.sendFile(Object.assign(oldMessage, {
    scene: 'p2p',
    to: 'account',
    type: 'image',
    fileInput: 'domId',
    resend: true, // 注意这个 resend 标记为 true,才能固定使用 oldMessage 里的 idClient
    done: function(err, obj) {
    if (err) {
    console.log('发送失败', err)
    } else {
    console.log('发送消息成功,消息为: ', obj)
    }
    }
    }))
    }

    先previewFile,再发送文件

    nim.previewFile({
    type: 'image',
    fileInput: fileInput,
    uploadprogress: function(obj) {
    console.log('文件总大小: ' + obj.total + 'bytes');
    console.log('已经上传的大小: ' + obj.loaded + 'bytes');
    console.log('上传进度: ' + obj.percentage);
    console.log('上传进度文本: ' + obj.percentageText);
    },
    done: function(error, file) {
    console.log('上传image' + (!error?'成功':'失败'));
    // show file to the user
    if (!error) {
    var msg = nim.sendFile({
    scene: 'p2p',
    to: 'account',
    file: file,
    done: sendMsgDone
    });
    console.log('正在发送p2p image消息, id=' + msg.idClient);
    pushMsg(msg);
    }
    }
    })

    关联链接

    Parameters

    Returns NIMMessage

  • 描述

    向目标用户、目标群组或目标超大群发送地理位置消息。

    注意

    该接口直接返回的是 sending 状态的未完成消息体,发送完毕的消息体需要传递 options.done 获得。

    影响范围

    调用该 API 可触发:

    1. 接收方的 NIMGetInstanceOptions.onmsg 回调函数
    2. 接收方的 NIMGetInstanceOptions.onupdatesessions 回调
    3. 发送方的 NIMGetInstanceOptions.onupdatesessions 回调
    4. 发送方同时在线的其它客户端 NIMGetInstanceOptions.onmsg 回调

    示例

    nim.sendGeo({
    scene: 'p2p',
    to: 'account',
    //接收方通过onMsg接收消息
    //然后如果msg.type === 'geo',接收方通过读取msg.geo,然后调用业务代码
    geo: {
    lng: 116.3833,
    lat: 39.9167,
    title: 'Beijing'
    },
    done: function(err, msg) {
    if (err) {
    console.log('发送失败', err)
    } else {
    console.log('发送消息成功,消息为: ', msg)
    }
    }
    })

    Parameters

    Returns NIMMessage

  • sendMsgReceipt(options: { msg: NIMMessage; done?: any }): void
  • sendTeamMsgReceipt(options: { teamMsgReceipts: { idClient?: string; idServer: string; teamId: string }[]; done?: any }): void
  • 描述

    群消息接收发送消息已读回执。发送方如果在线且已在初始化时注册 onTeamMsgReceipt ,会收到群消息已读的通知

    注意

    注意,发送者必须设置needMsgReceipt = true,接收者才能够发送群消息已读回执

    群消息已读回执分有两种接收方式:

    1. 初始化登录时,在同步阶段下发会话的已读回执时间, 必须设置NIMGetInstanceOptions.syncMsgReceipts = true,才能够接收
    2. 在线时,如果其它用户发送已读回执,通过NIMGetInstanceOptions.onTeamMsgReceipt 监听会话的最新已读回执时间

    影响范围

    调用该 API 发送成功时可触发: 回执接收方的 onTeamMsgReceipt 回调。

    示例场景

    示例

    //消息发送方
    nim.sendText({
    scene: 'team',
    to: 'teamId',
    text: "Hello",
    //注意,该字段必须设置为true
    needMsgReceipt: true
    })

    //消息接收方
    nim.sendTeamMsgReceipt({
    teamMsgReceipts: [{
    teamId: 'teamId',
    idClient: 'xxxx',
    idServer: 'yyyy'
    }]
    })

    Parameters

    • options: { teamMsgReceipts: { idClient?: string; idServer: string; teamId: string }[]; done?: any }
      • teamMsgReceipts: { idClient?: string; idServer: string; teamId: string }[]

        待发回执的群消息列表。

      • done?:function

    Returns void

  • 描述

    向目标用户、目标群组或目标超大群发送提示消息。提示消息主要用于会话内的通知提醒,典型业务场景包括进入群组时出现的欢迎消息和会话过程中命中敏感词后的提示等。

    注意

    该接口直接返回的是 sending 状态的未完成消息体,发送完毕的消息体需要传递 options.done 获得。

    影响范围

    调用该 API 可触发:

    1. 接收方的 NIMGetInstanceOptions.onmsg 回调函数
    2. 接收方的 NIMGetInstanceOptions.onupdatesessions 回调
    3. 发送方的 NIMGetInstanceOptions.onupdatesessions 回调
    4. 发送方同时在线的其它客户端 NIMGetInstanceOptions.onmsg 回调

    示例

    nim.sendTipMsg({
    scene: 'p2p',
    to: 'account',
    //接收方通过onMsg接收消息
    //然后如果msg.type === 'tip',接收方通过读取msg.tip,然后调用业务代码
    tip: 'tip content',
    done: function(err, msg) {
    if (err) {
    console.log('发送失败', err)
    } else {
    console.log('发送消息成功,消息为: ', msg)
    }
    }
    })

    Parameters

    Returns NIMMessage