数据: clockTime:17:11 ; clockMode:周五.周六.周日 ; clockRemember:Jkk
- (void)postLocalNotification:(NSString *)clockID isFirst:(BOOL)flag
{
//-----获取闹钟数据-----
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *clockDictionary = [userDefault objectForKey:clockID];
NSString *clockTime = [clockDictionary objectForKey:@"ClockTime"];
NSString *clockMode = [clockDictionary objectForKey:@"ClockMode"];
//NSString *clockMusic = [clockDictionary objectForKey:@"ClockMusic"];
NSString *clockRemember = [clockDictionary objectForKey:@"ClockRemember"];
if([clockMode isEqualToString:@"每天"]){
clockMode=@"周日.周一.周二.周三.周四.周五.周六";
}
//-----组建本地通知的fireDate-----
NSArray *clockTimeArray = [clockTime componentsSeparatedByString:@":"];
NSDate *dateNow = [NSDate date];
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
comps = [[NSDateComponents alloc] init];
//[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
//[comps setTimeZone:[NSTimeZone timeZoneWithName:@"CMT"]];
NSInteger unitFlags =
NSCalendarUnitEra| //纪元单位
NSCalendarUnitYear | //年单位
NSCalendarUnitMonth | //月单位,范围为1-12
NSCalendarUnitDay | //天单位,范围为1-31
NSCalendarUnitHour | //小时单位,范围为0-24
NSCalendarUnitMinute | //分钟单位,范围为0-60
NSCalendarUnitSecond | //秒单位,范围为0-60
NSCalendarUnitWeekOfYear | //年包含的周数,最多为53周
NSCalendarUnitWeekday | //星期单位,范围为1-7
NSCalendarUnitWeekdayOrdinal | //
NSCalendarUnitQuarter; //几刻种,也就是15分钟,范围为1-4
comps = [calendar components:unitFlags fromDate:dateNow];
[comps setHour:[[clockTimeArray objectAtIndex:0] intValue]];
[comps setMinute:[[clockTimeArray objectAtIndex:1] intValue]];
[comps setSecond:0];
NSArray *array = [clockMode componentsSeparatedByString:@"."];
for(int i=0; i<array.count;i++){
NSLog(@"array: %@",[array objectAtIndex:i]);
}
Byte i = 0;
Byte j = 0;
int days = 0;
int temp = 0;
Byte count = [array count];
Byte clockDays[7];
NSArray *tempWeekdays = [NSArray arrayWithObjects:@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
//查找设定的周期模式
for (i = 0; i < count; i++) {
for (j = 0; j < 7; j++) {
if ([[array objectAtIndex:i] isEqualToString:[tempWeekdays objectAtIndex:j]]) {
clockDays[i] = j + 1;
break;
}
}
}
Byte weekday = [comps weekday];
for (i = 0; i < count; i++) {
temp = clockDays[i] - weekday;
days = (temp >= 0 ? temp : temp + 7);
newFireDate = [[calendar dateFromComponents:comps] dateByAddingTimeInterval:86400* days];
UILocalNotification *newNotification = [[UILocalNotification alloc] init];
if (newNotification) {
//推送的最长显示时间是30s,要最长显示的话放一个大概是30s的音乐文件即可
newNotification.fireDate = newFireDate;
newNotification.alertBody = clockRemember; //提示信息
newNotification.soundName = @"music.wav"; //自定义提示音
newNotification.alertAction = @"查看闹钟"; //提示框按钮
newNotification.repeatInterval = NSCalendarUnitWeekOfYear; //循环次数,一周一次(每年)
NSMutableDictionary *mdic=[[NSMutableDictionary alloc]initWithCapacity:0];
// NSDictionary *userInfo = [NSDictionary dictionaryWithObject:clockID forKey:@"ActivityClock"];
mdic[@"name"]=clockRemember;
mdic[@"ActivityClock"]=clockID;
newNotification.userInfo = mdic; //添加额外的信息
[[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
}
NSLog(@"Post new localNotification:%@", [newNotification fireDate]);
}
}
取消闹钟:
/关闭闹钟
- (void)shutdownClock:(int)clockID
{
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for(UILocalNotification *notification in localNotifications)
{
if ([[[notification userInfo] objectForKey:@"ActivityClock"] intValue] == clockID) {
NSLog(@"Shutdown localNotification:%@", [notification fireDate]);
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}