//開啟免打擾
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);
}
}];
//關(guān)閉免打擾
MPushOptions *options = [[EMClient sharedClient] pushOptions];
options.noDisturbStatus = EMPushNoDisturbStatusClose; //關(guān)閉免打擾模式
[[EMClient sharedClient] updatePushNotificationOptionsToServerWithCompletion:^(EMError *aError) {
if (!aError) {
NSLog(@"關(guān)閉免打擾成功");
}else{
NSLog(@"error:%@", aError);
}
}];
以上設(shè)置完后,你會(huì)發(fā)現(xiàn)當(dāng)開啟免打擾后仍然可以收到推送,那是因?yàn)檫€需要在playSoundAndVibration
和showNotificationWithMessage:(EMMessage *)message
方法中進(jìn)行推送免打擾設(shè)置狀態(tài)的判斷击纬,當(dāng)狀態(tài)為EMPushNoDisturbStatusClose
時(shí)才執(zhí)行推送或播放提示聲音冯袍。
- (void)playSoundAndVibration
{
EMPushOptions *options = [[EMClient sharedClient] pushOptions];
if (options.noDisturbStatus == EMPushNoDisturbStatusClose) {
NSTimeInterval timeInterval = [[NSDate date]
timeIntervalSinceDate:self.lastPlaySoundDate];
if (timeInterval < kDefaultPlaySoundInterval) {
//如果距離上次響鈴和震動(dòng)時(shí)間太短, 則跳過響鈴
NSLog(@"skip ringing & vibration %@, %@", [NSDate date], self.lastPlaySoundDate);
return;
}
//保存最后一次響鈴時(shí)間
self.lastPlaySoundDate = [NSDate date];
// 收到消息時(shí),播放音頻
[[EMCDDeviceManager sharedInstance] playNewMessageSound];
// 收到消息時(shí),震動(dòng)
[[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]; //觸發(fā)通知的時(shí)間
notification.alertBody = alertBody;
notification.alertAction = NSLocalizedString(@"open", @"Open");
notification.timeZone = [NSTimeZone defaultTimeZone];//時(shí)區(qū)
if (playSound) {
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.userInfo = userInfo;
//發(fā)送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
---end---