//开启免打扰
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);
}
}];
以上设置完后,你会发现当开启免打扰后仍然可以收到推送,那是因为还需要在playSoundAndVibration
和showNotificationWithMessage:(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---