即时通讯
客户端 SDK
发布日志
升级指南
当前页

设置会话消息定时销毁

2026-07-24

功能概述

说明

如需使用本功能,请开通旗舰版套餐。

设置会话消息定时销毁,指设置一个会话的消息定时销毁时长后,该会话内此后发送的消息将在指定时长后自动删除。可用于阅后即焚、保密通信等场景。

消息定时销毁.png

设置会话消息定时销毁时长

调用 setConversationMessageDestructDuration 接口,可设置指定会话的消息定时销毁时长。

参数说明:

  • duration:定时销毁时长,单位秒。支持设置的范围为 060 ~ 604800(7 天)。设置为 0 表示取消定时销毁。

调用时机: 用户登录后可调用,网络条件良好时可调用成功。

  • 设置单聊会话消息定时销毁,需要 conversationID 对应的用户为已注册用户。
  • 设置群聊会话消息定时销毁,需要群组已创建、且当前用户已加入群组。
    注意

    群聊会话消息的定时销毁设置默认为所有群成员均可配置,支持修改为仅群主和管理员可配置。如需修改默认权限配置,请联系 ZEGO 技术支持。

// duration: 单位秒。支持设置的范围 0 或 60-604800(7d),0 = 取消定时销毁
int duration = 300; // 例如设置为 5 分钟后销毁

zim.setConversationMessageDestructDuration(duration, conversationID, ZIMConversationType.PEER,
    new ZIMConversationMessageDestructDurationSetCallback() {
        @Override
        public void onConversationMessageDestructDurationSet(
            String conversationID, ZIMConversationType conversationType, ZIMError errorInfo) {
            if (errorInfo.code == ZIMErrorCode.SUCCESS) {
                // 设置成功
            } else {
                // 设置失败
            }
        }
    });

获取会话消息的定时销毁配置

当会话的定时销毁时长发生变化时(包括本端设置或多端同步),可通过 onConversationChanged 回调感知到变化。

ZIMConversation 新增 messageDestructDuration 属性,表示当前会话的限时消息时长。未设置过定时销毁的会话,该值为 0

// 1. 监听会话变更事件
zim.setEventHandler(new ZIMEventHandler() {
    @Override
    public void onConversationChanged(ZIM zim, ArrayList<ZIMConversationChangeInfo> conversationChangeInfoList) {
        for (ZIMConversationChangeInfo info : conversationChangeInfoList) {
            int duration = info.conversation.messageDestructDuration;
            // 根据 duration 更新 UI
        }
    }
});

// 2. 也可通过 queryConversation 接口查询指定会话
zim.queryConversation(conversationID, ZIMConversationType.PEER, new ZIMConversationQueriedCallback() {
    @Override
    public void onConversationQueried(ZIMConversation conversation, ZIMError errorInfo) {
        if (errorInfo.code == ZIMErrorCode.SUCCESS) {
            int duration = conversation.messageDestructDuration;
        }
    }
});

获取限时消息的过期时间戳

在会话内设置消息定时销毁时长后,此后该会话内成员发送的消息,将会携带过期时间戳 destructTimedestructTime 单位为毫秒,0 表示该消息非限时消息。

开发者可通过 destructTime 和当前时间,获取对应消息的销毁倒计时。

// ZIMMessage 新增属性
// public long destructTime; // 单位毫秒,0 表示非限时消息

long destructTime = message.destructTime;
if (destructTime > 0) {
    long countdown = destructTime - System.currentTimeMillis();
    // 展示倒计时
}

获取更精确的消息销毁倒计时

若希望获得更精确的倒计时,可通过 callExperimentalAPI 接口获取 SDK 当前时间戳,再通过 destructTime - SDK 当前时间戳 获得倒计时。

传入参数为 JSON 字符串:

{
    "method": "getSDKTimestamp"
}

SDK 返回结果(ZIMExperimentalAPICalledCallback 的 result 参数)为 JSON:

{
    "timestamp": 123456
}

其中 timestamp 精度为毫秒。

监听定时销毁消息

当消息达到销毁时间时,将会触发 onMessageDeleted 事件,删除类型 ZIMMessageDeleteTypeMessagesDestructed

开发者可监听此回调,从消息列表中移除对应的消息。

// ZIMMessageDeleteType 枚举
// MessageListDeleted             = 0  // 用户主动删除
// ConversationAllMessagesDeleted  = 1  // 清空会话消息
// AllConversationMessagesDeleted  = 2  // 清空所有会话
// MessagesDestructed             = 3  // 消息定时销毁

zim.setEventHandler(new ZIMEventHandler() {
    @Override
    public void onMessageDeleted(ZIM zim, ZIMMessageDeletedInfo deletedInfo) {
        if (deletedInfo.deleteType == ZIMMessageDeleteType.MessagesDestructed) {
            // 消息因定时销毁被删除
            for (ZIMMessage message : deletedInfo.messageList) {
                // 从消息列表中移除对应消息
            }
        }
    }
});

上一篇

标记会话

下一篇

功能概述

当前页

返回到顶部