功能描述
通过设置单聊、群聊或者全局的消息接收选项,可以实现类似“微信”或者“QQ”中消息免打扰的功能。
IM SDK 提供三种类型的消息接收选项,在
V2TIMReceiveMessageOpt
中定义如下。单聊设置除第四个选项全部支持,群聊设置支持全部选项,全局设置仅支持第一和第三项。消息接收选项 | 功能描述 |
V2TIM_RECEIVE_MESSAGE | 在线时正常接收消息,离线时接收离线推送通知 |
V2TIM_NOT_RECEIVE_MESSAGE | 在线和离线都不接收消息 |
V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE | 在线时正常接收消息,离线时不接收离线推送通知 |
V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE_EXCEPT_AT | 在线接收消息,离线只接收 at 消息的推送 |
V2TIM_NOT_RECEIVE_MESSAGE_EXCEPT_AT | 在线和离线只接收 at 消息 |
说明
单聊和群聊消息接收选项仅增强版 SDK 5.3.425 及以上版本支持;全局消息接收选项仅增强版 SDK 7.4.4643 及以上版本支持。
设置不同的
V2TIMReceiveMessageOpt
可以实现不同的消息免打扰效果:完全不接收消息
仅适用单聊/群聊场景,消息接收选项设置为
V2TIM_NOT_RECEIVE_MESSAGE
,任何消息都收不到,会话列表也不会更新。接收消息但不提醒
消息接收选项设置为
V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE
。单聊/群聊设置场景,在会话列表界面显示小圆点(不显示未读数):
1.1 当收到新消息,会话列表需要更新时,通过会话 V2TIMConversation 中的 unreadCount (Android / iOS & Mac / Windows) 获取到消息未读数。
1.2 根据
V2TIMConversation
的 recvOpt
(Android / iOS & Mac / Windows) 判断获取到的消息接收选项为 V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE
时显示小红点而非消息未读数。接收消息但只提醒 at 消息
消息接收选项设置为
V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE_EXCEPT_AT
。全局设置场景:
1.1 指定时间窗口内,登录账号所有消息在线可以收到,离线不进行推送。
1.2 可根据接口
getAllReceiveMessageOpt
(Android / iOS & Mac / Windows) ,获取全局消息接收状态 V2TIMReceiveMessageOptInfo (Android / iOS & Mac / Windows) ,根据设置的时间窗口和接收选项来决定上层 UI 显示。只接收 at 消息
仅适用话题场景,消息接收选项设置为
V2TIM_NOT_RECEIVE_MESSAGE_EXCEPT_AT
,在线和离线都只接收 at 消息。说明
此方式单聊/群聊需使用
unreadCount
功能,仅适用于好友工作群(Work)、陌生人社交群(Public)和社群(Community),直播群(AVChatRoom)、临时会议群(Meeting)暂不支持。群组类型详见 群组介绍。设置单聊的消息接收选项
您可以调用
setC2CReceiveMessageOpt
(Android / iOS & Mac / Windows) 设置单聊的消息接收选项。
您可通过参数 userIDList
设置一批用户,但一次最大允许设置 30 个用户。注意
该接口调用频率被限制为单个用户 1 秒内最多调用 5 次。
示例代码如下:
// 设置在线和离线都不接收消息List<String> userList = new ArrayList<>();userList.add("user1");userList.add("user2");V2TIMManager.getMessageManager().setC2CReceiveMessageOpt(userList, V2TIMMessage.V2TIM_NOT_RECEIVE_MESSAGE, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
// 设置在线和离线都不接收消息NSArray* array = [NSArray arrayWithObjects:@"user1", @"user2", nil]];[[V2TIMManager sharedInstance] setC2CReceiveMessageOpt:array opt:V2TIM_NOT_RECEIVE_MESSAGE succ:^{NSLog(@"success");} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector userIDList;userIDList.PushBack("user1");userIDList.PushBack("user2");V2TIMReceiveMessageOpt opt = V2TIMReceiveMessageOpt::V2TIM_NOT_RECEIVE_MESSAGE;auto callback = new Callback;callback->SetCallback([=]() {// 设置成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 设置失败delete callback;});V2TIMManager::GetInstance()->GetMessageManager()->SetC2CReceiveMessageOpt(userIDList, opt, callback);
获取单聊的消息接收选项
示例代码如下:
List<String> userList = new ArrayList<>();userList.add("user1");userList.add("user2");V2TIMManager.getMessageManager().getC2CReceiveMessageOpt(userList, new V2TIMValueCallback<List<V2TIMReceiveMessageOptInfo>>() {@Overridepublic void onSuccess(List<V2TIMReceiveMessageOptInfo> v2TIMReceiveMessageOptInfos) {for (int i = 0; i < v2TIMReceiveMessageOptInfos.size(); i++){V2TIMReceiveMessageOptInfo info = v2TIMReceiveMessageOptInfos.get(i);Log.i("imsdk", "userId: " + info.getUserID() ", receiveOpt: " + info.getC2CReceiveMessageOpt());}}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
NSArray* array = [NSArray arrayWithObjects:@"user1", @"user2", nil]];[[V2TIMManager sharedInstance] getC2CReceiveMessageOpt:array succ:^(NSArray<V2TIMReceiveMessageOptInfo *> *optList) {for (int i = 0; i < optList.count; i++) {V2TIMReceiveMessageOptInfo* info = optList[i];NSLog(@"userId: %@, receiveOpt: %@", info.userID, info.receiveOpt);}} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector userIDList;userIDList.PushBack("user1");userIDList.PushBack("user2");auto callback = new ValueCallback<V2TIMReceiveMessageOptInfoVector>{};callback->SetCallback([=](const V2TIMReceiveMessageOptInfoVector& receiveMessageOptInfoList) {for (size_t i = 0; i < receiveMessageOptInfoList.Size(); ++i) {const V2TIMReceiveMessageOptInfo& opt = receiveMessageOptInfoList[i];V2TIMString userID = opt.userID;V2TIMReceiveMessageOpt receiveOpt = opt.receiveOpt;}delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取失败delete callback;});V2TIMManager::GetInstance()->GetMessageManager()->GetC2CReceiveMessageOpt(userIDList, callback);
设置群聊的消息接收选项
示例代码如下:
// 设置在线和离线都不接收消息String groupID = "groupID";V2TIMManager.getMessageManager().setGroupReceiveMessageOpt(groupID, V2TIMMessage.V2TIM_NOT_RECEIVE_MESSAGE, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
// 设置在线和离线都不接收消息NSString *groupID = @"groupID";[[V2TIMManager sharedInstance] setGroupReceiveMessageOpt:groupID opt:V2TIM_NOT_RECEIVE_MESSAGE succ:^{NSLog(@"success");} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString groupID = "groupID";V2TIMReceiveMessageOpt opt = V2TIMReceiveMessageOpt::V2TIM_NOT_RECEIVE_MESSAGE;auto callback = new Callback;callback->SetCallback([=]() {// 设置成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 设置失败delete callback;});V2TIMManager::GetInstance()->GetMessageManager()->SetGroupReceiveMessageOpt(groupID, opt, callback);
获取群聊的消息接收选项
您可以调用
getGroupsInfo
(Android / iOS & Mac / Windows) 获得群资料 V2TIMGroupInfo
对象列表,对象的 recvOpt
字段表示群组的消息接收选项。示例代码如下:
List<String> groupIDList = new ArrayList<>();groupIDList.add("groupID1");groupIDList.add("groupID2");V2TIMManager.getGroupManager().getGroupsInfo(groupIDList, new V2TIMValueCallback<List<V2TIMGroupInfoResult>>() {@Overridepublic void onSuccess(List<V2TIMGroupInfoResult> v2TIMGroupProfileResults) {for (V2TIMGroupInfoResult result : v2TIMGroupProfileResults) {V2TIMGroupInfo info = result.getGroupInfo();Log.i("imsdk", "recvOpt: " + info.getRecvOpt());}}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
NSArray* array = [NSArray arrayWithObjects:@"groupID1", @"groupID2", nil]];[[V2TIMManager sharedInstance] getGroupsInfo:array succ:^(NSArray<V2TIMGroupInfoResult *> * groupResultList) {for (V2TIMGroupInfoResult *result in groupResultList){V2TIMGroupInfo *info = result.info;NSLog(@"recvOpt, %d", (int)info.recvOpt);}} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector groupIDList;groupIDList.PushBack("groupID1");groupIDList.PushBack("groupID2");auto callback = new ValueCallback<V2TIMGroupInfoResultVector>{};callback->SetCallback([=](const V2TIMGroupInfoResultVector& groupInfoResultList) {for (size_t i = 0; i < groupInfoResultList.Size(); ++i) {const V2TIMGroupInfo& info = groupInfoResultList[i].info;V2TIMReceiveMessageOpt recvOpt = info.recvOpt;}delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取失败delete callback;});V2TIMManager::GetInstance()->GetGroupManager()->GetGroupsInfo(groupIDList, callback);
设置全局的消息接收选项
您可以调用
setAllReceiveMessageOpt
(Android / iOS & Mac / Windows) 设置登录账号的全局消息接收选项,支持设置每天重复生效,也支持指定时间段内生效。示例代码如下:
// 设置登录账号每天晚上 10 点到早上 6 点,在线正常接收消息,离线不会有推送通知V2TIMManager.getMessageManager().setAllReceiveMessageOpt(V2TIMMessage.V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE, 22, 0, 0, 8*60*60, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.d("imsdk", "setAllReceiveMessageOpt onSuccess");}@Overridepublic void onError(int code, String desc) {Log.d("imsdk", "setAllReceiveMessageOpt onError code = " + code + ", desc = " + desc);}});// 假设今天晚上 10 点的 UTC 时间是 12345678。// 设置登录账号从今天晚上 10 点开始,连续三天时间,在线正常接收消息,离线不会有推送通知V2TIMManager.getMessageManager().setAllReceiveMessageOpt(V2TIMMessage.V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE, 12345678, 3*24*60*60, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.d("imsdk", "setAllReceiveMessageOpt onSuccess");}@Overridepublic void onError(int code, String desc) {Log.d("imsdk", "setAllReceiveMessageOpt onError code = " + code + ", desc = " + desc);}});
// 设置登录账号每天晚上 10 点到早上 6 点,在线正常接收消息,离线不会有推送通知[[V2TIMManager sharedInstance] setAllReceiveMessageOpt:V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE startHour:22 startMinute:0 startSecond:0 duration:8*60*60 succ:^{NSLog(@"success");} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];// 假设今天晚上 10 点的 UTC 时间是 12345678。// 设置登录账号从今天晚上 10 点开始,连续三天时间,在线正常接收消息,离线不会有推送通知[[V2TIMManager sharedInstance] setAllReceiveMessageOpt:V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE startTimeStamp:12345678 duration:3*24*60*60 succ:^{NSLog(@"success");} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMReceiveMessageOpt opt = V2TIMReceiveMessageOpt::V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE;auto callback = new Callback;callback->SetCallback([=]() {// 设置成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 设置失败delete callback;});// 设置登录账号每天晚上 10 点到早上 6 点,在线正常接收消息,离线不会有推送通知V2TIMManager::GetInstance()->GetMessageManager()->SetAllReceiveMessageOpt(opt, 22, 0, 0, 8*60*60, callback);// 假设今天晚上 10 点的 UTC 时间是 12345678。// 设置登录账号从今天晚上 10 点开始,连续三天时间,在线正常接收消息,离线不会有推送通知V2TIMManager::GetInstance()->GetMessageManager()->SetAllReceiveMessageOpt(opt, 12345678, 3*24*60*60, callback);
获取全局的消息接收选项
您可以调用
getAllReceiveMessageOpt
(Android / iOS & Mac / Windows) 获得全局消息接收选项 V2TIMReceiveMessageOptInfo
对象,表示登录账号的全局消息接收选项信息。示例代码如下:
V2TIMManager.getMessageManager().getAllReceiveMessageOpt(new V2TIMValueCallback<V2TIMReceiveMessageOptInfo>() {@Overridepublic void onSuccess(V2TIMReceiveMessageOptInfo v2TIMReceiveMessageOptInfo) {String result = "startHour = " + v2TIMReceiveMessageOptInfo.getStartHour() +", startMinute = " + v2TIMReceiveMessageOptInfo.getStartMinute() +", startSecond = " + v2TIMReceiveMessageOptInfo.getStartSecond() +", startTimeStamp = " + v2TIMReceiveMessageOptInfo.getStartTimeStamp() +", duration = " + v2TIMReceiveMessageOptInfo.getDuration() +", opt = " + v2TIMReceiveMessageOptInfo.getAllReceiveMessageOpt();Log.d("imsdk", "getAllReceiveMessageOpt onSuccess = " + result);}@Overridepublic void onError(int code, String desc) {Log.d("imsdk", "getAllReceiveMessageOpt onError code = " + code + ", desc = " + desc);}});
[V2TIMManager.sharedInstance getAllReceiveMessageOpt:^(V2TIMReceiveMessageOptInfo *optInfo) {NSLog(@"optInfo %@", optInfo);} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new ValueCallback<V2TIMReceiveMessageOptInfo>{};callback->SetCallback([=](const V2TIMReceiveMessageOptInfo& receiveMessageOptInfo) {delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取失败delete callback;});V2TIMManager::GetInstance()->GetMessageManager()->GetAllReceiveMessageOpt(callback);