• 【设定本地通知为周一到周五提醒, 周末不提醒解决办法】


    iOS开发中的信息提示推送方式,一类是远程服务器推送(APNS)与UILocalNotification本地通知的,我们知道UILocalNotification的通知重复提示的单位是以是秒、分、时、天、周、月等. 如图:

     

     

     

    那么问题来了, 要实现标题所说的该怎么办呢? 我的方法为判断当前为星期几, 然后设定5个以周为单位的本地通知. LOOK...

    + (BOOL)setWeekdayLocalNotificationWithHour:(int)notificationHour
    {
        NSInteger year,month,day,hour,min,sec,week;
    
        NSString *weekStr=nil;
    
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDate *now = [NSDate date];
    
        NSDateComponents *comps = [[NSDateComponents alloc] init];
    
        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    
        NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
        comps = [calendar components:unitFlags fromDate:now];
    
        year = [comps year];
    
        week = [comps weekday];
    
        month = [comps month];
    
        day = [comps day];
    
        hour = [comps hour];
    
        min = [comps minute];
    
        sec = [comps second];
    NSTimeInterval oneDayay
    = 24 * 3600; BOOL isWeekend = NO; if(week == 1) { weekStr=@"星期天"; isWeekend = YES; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 3] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 4] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 5] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; }else if(week == 2){ weekStr=@"星期一"; [self setLocalNotificationWithFireDate:now withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 3] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 4] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; }else if(week == 3){ weekStr=@"星期二"; [self setLocalNotificationWithFireDate:now withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 3] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; }else if(week == 4){ weekStr=@"星期三"; [self setLocalNotificationWithFireDate:now withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; }else if(week==5){ weekStr=@"星期四"; [self setLocalNotificationWithFireDate:now withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 3] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; }else if(week == 6){ weekStr=@"星期五"; [self setLocalNotificationWithFireDate:now withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 3] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 4] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; }else if(week == 7){ weekStr=@"星期六"; isWeekend = YES; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 2] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 3] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 4] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; [self setLocalNotificationWithFireDate:[now dateByAddingTimeInterval:-oneDayay * 5] withTitle:@"哈哈哈..." withNotificationHour:notificationHour]; } NSLog(@"现在是:%ld年%ld月%ld日 %ld时%ld分%ld秒 %@", (long)year,(long)month,(long)day,(long)hour,(long)min,(long)sec,weekStr); return isWeekend; } #pragma mark 本地通知 + (void)setLocalNotificationWithFireDate:(NSDate *)mFireDate withTitle:(NSString *)alertBody withNotificationHour:(int)hour { UILocalNotification *mLocalNotification = [[UILocalNotification alloc] init]; // 设置通知的标题(操作名称) mLocalNotification.alertAction = @"休息休息!"; // 设置通知的正文 mLocalNotification.alertBody = alertBody; NSDateComponents *comps = [[NSDateComponents alloc] init] ; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:mFireDate]; [comps setHour:hour]; [comps setMinute:21]; [comps setSecond:0]; NSDate *newFireDate = [[NSCalendar currentCalendar] dateFromComponents:comps]; mLocalNotification.fireDate = newFireDate; NSLog(@"mFireDate: %@-- %d--%@", newFireDate, hour, [self DateFromString:@"2015-09-14 13:41:00"]); // 设置重复通知的时间为每日提醒 mLocalNotification.repeatInterval = kCFCalendarUnitWeek; // kCFCalendarUnitMinute NSCalendarUnitDay // 通知触发时播放的声音 mLocalNotification.soundName = UILocalNotificationDefaultSoundName; // 取消通知的时候判断key和ID相同的就是同一个通知了。 NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d", hour], @"key" ,nil]; [mLocalNotification setUserInfo:dict]; // 利用Application添加通知 [[UIApplication sharedApplication] scheduleLocalNotification:mLocalNotification]; } /** 取消某个课程的通知 如: value = 10 */ + (void)cancelLocalNotificationWithkeyValue:(NSString *)value { NSArray *notifications=[[UIApplication sharedApplication] scheduledLocalNotifications]; NSUInteger acount = notifications.count; if (acount>0){ for (UILocalNotification *myUILocalNotification in notifications) { NSDictionary *userInfo = myUILocalNotification.userInfo; if ([value isEqualToString:userInfo[@"key"]]) { [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; NSLog(@"取消: %@", value); } } } }

    好了, 赶紧试试吧! 如果还有更好的方法, 请告知, TKS! 

     

  • 相关阅读:
    基于WebDriverAgent代理服务,实现iOS手机app自动化测试的框架搭建
    ruby 中%Q %q %W %w %x %r %s的用法
    ruby中 Win32ole的各应用操作方法(word excel Outlook IE)
    dubbo服务自动化测试搭建
    Spring AOP 结合自定义注解的使用
    Mysql 中 JSON_CONTAINS、JSON_ARRAY 的使用
    Spring Cloud Eureka 的一些配置
    Spring Cloud 整合 Spring Boot Admin
    Nginx 泛域名解析配置
    Spring Data Solr
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/4798627.html
Copyright © 2020-2023  润新知