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


    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! 

     

  • 相关阅读:
    C#使用二叉树算法设计一个无限分级的树表
    程序员写博客这件小事
    jqgrid定义多选操作
    jqgrid如何在一个页面点击按钮后,传递参数到新页面
    MVC 移除复数表名的契约
    [技术分享] .NET下 , 上传图片的处理方式 , 贴上代码 .
    Web应用程序项目以配置使用IIS。未找到Web服务器
    MVC5关联表读取相关表数据
    【转】C# Linq 交集、并集、差集、去重
    .NET MVC3中扩展一个HtmlHelper方法CheckBoxList
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/4798627.html
Copyright © 2020-2023  润新知