iOS之环信开启/关闭免打扰

//开启免打扰
MPushOptions *options = [[EMClient sharedClient] pushOptions];
options.noDisturbStatus = EMPushNoDisturbStatusDay;  //全天免打扰
options.noDisturbingStartH = 0;
options.noDisturbingEndH = 24;
[[EMClient sharedClient] updatePushNotificationOptionsToServerWithCompletion:^(EMError *aError) {
    if (!aError) {
        NSLog(@"开启免打扰成功");
    }else{
        NSLog(@"error:%@", aError);
    }
}];

//关闭免打扰
MPushOptions *options = [[EMClient sharedClient] pushOptions];
options.noDisturbStatus = EMPushNoDisturbStatusClose;  //关闭免打扰模式
[[EMClient sharedClient] updatePushNotificationOptionsToServerWithCompletion:^(EMError *aError) {
    if (!aError) {
        NSLog(@"关闭免打扰成功");
    }else{
        NSLog(@"error:%@", aError);
    }
}];

以上设置完后,你会发现当开启免打扰后仍然可以收到推送,那是因为还需要在playSoundAndVibrationshowNotificationWithMessage:(EMMessage *)message方法中进行推送免打扰设置状态的判断,当状态为EMPushNoDisturbStatusClose时才执行推送或播放提示声音。

- (void)playSoundAndVibration
{
    EMPushOptions *options = [[EMClient sharedClient] pushOptions];
    if (options.noDisturbStatus == EMPushNoDisturbStatusClose) {
        NSTimeInterval timeInterval = [[NSDate date]
                                       timeIntervalSinceDate:self.lastPlaySoundDate];
        if (timeInterval < kDefaultPlaySoundInterval) {
            //如果距离上次响铃和震动时间太短, 则跳过响铃
            NSLog(@"skip ringing & vibration %@, %@", [NSDate date], self.lastPlaySoundDate);
            return;
        }
        
        //保存最后一次响铃时间
        self.lastPlaySoundDate = [NSDate date];
        
        // 收到消息时,播放音频
        [[EMCDDeviceManager sharedInstance] playNewMessageSound];
        // 收到消息时,震动
        [[EMCDDeviceManager sharedInstance] playVibration];
    }
}

- (void)showNotificationWithMessage:(EMMessage *)message
{
    EMPushOptions *options = [[EMClient sharedClient] pushOptions];
    if (options.noDisturbStatus == EMPushNoDisturbStatusClose) {
        NSString *alertBody = @"您有一条新消息";
        
        NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate];
        BOOL playSound = NO;
        if (!self.lastPlaySoundDate || timeInterval >= kDefaultPlaySoundInterval) {
            self.lastPlaySoundDate = [NSDate date];
            playSound = YES;
        }
        
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        [userInfo setObject:[NSNumber numberWithInt:message.chatType] forKey:kMessageType];
        [userInfo setObject:message.conversationId forKey:kConversationChatter];
        
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.fireDate = [NSDate date]; //触发通知的时间
        notification.alertBody = alertBody;
        notification.alertAction = NSLocalizedString(@"open", @"Open");
        notification.timeZone = [NSTimeZone defaultTimeZone];//时区
        if (playSound) {
            notification.soundName = UILocalNotificationDefaultSoundName;
        }
        notification.userInfo = userInfo;
        
        //发送通知
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
}

---end---

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容