iOS本地通知UILocalNotification–重复提醒(周一、周二、周三…)

释放双眼,带上耳机,听听看~!
// 初始化本地通知对象  newWeekDay为需要重复的星期

     UILocalNotification *notification = [[UILocalNotification
alloc] init];

     notification.timeZone = [NSTimeZone
defaultTimeZone];
// 使用本地时区

     NSDateComponents * components = [[NSCalendarcurrentCalendar] components:NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecondfromDate:[NSDatedate]];

     NSLog(@"设置的weekday
= %d", newWeekDay);

     int hour = [[timeArr
firstObject] intValue];

     int minute = [[timeArr
lastObject] intValue];

     NSDateComponents *comps = [[NSDateComponents
alloc] init] ;




     NSInteger unitFlags =
NSEraCalendarUnit |

NSYearCalendarUnit |

                NSMonthCalendarUnit |

NSDayCalendarUnit |

NSHourCalendarUnit |

                NSMinuteCalendarUnit |

                NSSecondCalendarUnit |

NSWeekCalendarUnit |

                NSWeekdayCalendarUnit |

                NSWeekdayOrdinalCalendarUnit |

                NSQuarterCalendarUnit;

     comps = [[NSCalendar
currentCalendar] components:unitFlags
fromDate:[NSDate
date]];

     [comps
setHour:hour];

     [comps
setMinute:minute];

     [comps
setSecond:0];

     int temp =
0;

     int days =
0;

     temp = newWeekDay - components.weekday;

     days = (temp >=
0 ? temp : temp +
7);

     NSDate *newFireDate = [[[NSCalendar
currentCalendar] dateFromComponents:comps]
dateByAddingTimeInterval:3600 *
24 * days];

     notification.fireDate = newFireDate;

     //
设置重复间隔

     notification.repeatInterval =
kCFCalendarUnitWeek;

     NSLog(@"具体启动的时间:%@",
notification.fireDate.description);

     //
设置提醒的文字内容

     notification.alertBody   =
@"亲,该称量体重了!";

     //
通知提示音使用默认的

     notification.soundName=
UILocalNotificationDefaultSoundName;

     NSDictionary *dic = [NSDictionary
dictionaryWithObject:[NSString
stringWithFormat:@"%@%@",
@"zhouleizhao", [AppDelegate
getInstance].currentLoginID]
forKey:@"key"];

     notification.userInfo = dic;

     //
将通知添加到系统中

     [[UIApplicationsharedApplication] scheduleLocalNotification:notification];

 

注:如果要实现周一、周二等同时重复提醒,需要添加多个以上通知来实现。

人已赞赏
iOS文章

Xcode-AppStore上线问题总结

2020-3-7 13:22:24

iOS文章

ios最新mupdf官方demo(mupdf库文件编译)编译运行

2020-3-7 15:22:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索