设置会话消息定时销毁
功能概述
如需使用本功能,请开通旗舰版套餐。
设置会话消息定时销毁,指设置一个会话的消息定时销毁时长后,该会话内此后发送的消息将在指定时长后自动删除。可用于阅后即焚、保密通信等场景。

设置会话消息定时销毁时长
调用 setConversationMessageDestructDuration 接口,可设置指定会话的消息定时销毁时长。
参数说明:
duration:定时销毁时长,单位秒。支持设置的范围为0或60~604800(7 天)。设置为0表示取消定时销毁。
调用时机: 用户登录后可调用,网络条件良好时可调用成功。
- 设置单聊会话消息定时销毁,需要
conversationID对应的用户为已注册用户。 - 设置群聊会话消息定时销毁,需要群组已创建、且当前用户已加入群组。
注意
群聊会话消息的定时销毁设置默认为所有群成员均可配置,支持修改为仅群主和管理员可配置。如需修改默认权限配置,请联系 ZEGO 技术支持。
// duration: 单位秒。支持设置的范围 0 或 60-604800(7d),0 = 取消定时销毁
int duration = 300; // 例如设置为 5 分钟后销毁
[zim setConversationMessageDestructDuration:duration
conversationID:conversationID
conversationType:ZIMConversationTypePeer
callback:^(NSString *conversationID, ZIMConversationType conversationType, ZIMError *errorInfo) {
if (errorInfo.code == 0) {
// 设置成功
} else {
// 设置失败
}
}];获取会话消息的定时销毁配置
当会话的定时销毁时长发生变化时(包括本端设置或多端同步),可通过 conversationChanged 回调感知到变化。
ZIMConversation 新增 messageDestructDuration 属性,表示当前会话的限时消息时长。未设置过定时销毁的会话,该值为 0。
// 1. 监听会话变更事件
- (void)zim:(ZIM *)zim conversationChanged:(NSArray<ZIMConversationChangeInfo *> *)infoList {
for (ZIMConversationChangeInfo *info in infoList) {
int duration = info.conversation.messageDestructDuration;
// 根据 duration 更新 UI
}
}
// 2. 也可通过 queryConversation 接口查询指定会话
[zim queryConversationWithConversationID:conversationID
conversationType:ZIMConversationTypePeer
callback:^(ZIMConversation *conversation, ZIMError *errorInfo) {
if (errorInfo.code == 0) {
int duration = conversation.messageDestructDuration;
}
}];获取限时消息的过期时间戳
在会话内设置消息定时销毁时长后,此后该会话内成员发送的消息,将会携带过期时间戳 destructTime。destructTime 单位为毫秒,0 表示该消息非限时消息。
开发者可通过 destructTime 和当前时间,获取对应消息的销毁倒计时。
// ZIMMessage 新增属性
// @property (nonatomic, assign) long long destructTime; // 单位毫秒,0 表示非限时消息
long long destructTime = message.destructTime;
if (destructTime > 0) {
long long countdown = destructTime - [[NSDate date] timeIntervalSince1970] * 1000;
// 展示倒计时
}获取更精确的消息销毁倒计时
若希望获得更精确的倒计时,可通过 callExperimentalAPIWithParams 接口获取 SDK 当前时间戳,再通过 destructTime - SDK 当前时间戳 获得倒计时。
传入参数为 JSON 字符串:
{
"method": "getSDKTimestamp"
}SDK 返回结果(ZIMExperimentalAPICalledCallback 的 result 参数)为 JSON:
{
"timestamp": 123456
}其中 timestamp 精度为毫秒。
监听定时销毁消息
当消息达到销毁时间时,将会触发 onMessageDeleted 事件,删除类型 ZIMMessageDeleteType 为 MessagesDestructed。
开发者可监听此回调,从消息列表中移除对应的消息。
// ZIMMessageDeleteType 枚举
// ZIMMessageDeleteTypeMessageListDeleted = 0 // 用户主动删除
// ZIMMessageDeleteTypeConversationAllMessagesDeleted = 1 // 清空会话消息
// ZIMMessageDeleteTypeAllConversationMessagesDeleted = 2 // 清空所有会话
// ZIMMessageDeleteTypeMessagesDestructed = 3 // 消息定时销毁
- (void)zim:(ZIM *)zim messageDeleted:(ZIMMessageDeletedInfo *)deletedInfo {
if (deletedInfo.deleteType == ZIMMessageDeleteTypeMessagesDestructed) {
// 消息因定时销毁被删除
for (ZIMMessage *message in deletedInfo.messageList) {
// 从消息列表中移除对应消息
}
}
}