您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页iOS之环信开启/关闭免打扰

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---

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务