iOS

消息过滤

更新时间: 2024/03/14 17:08:37

SDK 提供消息过滤忽略的功能。消息过滤后,SDK将不存储对应的消息,也不会上抛给接收回调,因此应用层不会收到对应的消息。

消息过滤仅对在线消息、离线消息、漫游消息等有效。本地和云端历史记录无法过滤。

使用场景

适用于过滤用户不需要关注的消息(如群头像变更通知消息),使用户免受过多信息的干扰。

实现流程

  1. 初始化 SDK 时,创建一个NIMSDKConfigDelegate对象,并实现下列方法。

    • -(BOOL)shouldIgnoreNotification:(NIMNotificationObject *)notification方法,返回YES表示忽略某条通知消息。
    • -(BOOL)shouldIgnoreMessage:(NIMMessage *)message方法,返回YES表示忽略某条普通消息。

    示例代码如下:

    - (BOOL)shouldIgnoreMessage:(NIMMessage *)message
    {
        NIMMessageType type = NIMMessageTypeRobot;
        if (message.messageType == type) {
            return YES;
        }
        return NO;
    }
    
  2. NIMSDKConfigDelegate对象赋值给NIMSDKConfigdelegate属性。

    不建议在消息过滤函数或方法中进行耗时操作,否则将导致线程阻塞。


    示例代码如下:

    - (BOOL)shouldIgnoreNotification:(NIMNotificationObject *)notification
    {
        BOOL ignore = NO;
        NIMNotificationContent *content = notification.content;
        if ([content isKindOfClass:[NIMTeamNotificationContent class]]) //这里做个示范如何忽略部分通知 (不在聊天界面显示)
        {
            NSArray *types = [[NTESBundleSetting sharedConfig] ignoreTeamNotificationTypes];
            NIMTeamOperationType type = [(NIMTeamNotificationContent *)content operationType];
            for (NSString *item in types)
            {
                if (type == [item integerValue])
                {
                    ignore = YES;
                    break;
                }
            }
        }
        return ignore;
    }
    
此文档是否对你有帮助?
有帮助
去反馈
  • 使用场景
  • 实现流程