本地消息使用(UILocalNotification,闹钟功能)

//时间换成NSString

- (void)randomPickerView:(DatePickView*)randomPickerView selectDate:(NSDate*)selectDate

{

    //07:01

//    NSLog(@"ssss %@", selectDate);

    NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

 

    NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];

    [calendar setTimeZone:[NSTimeZonetimeZoneWithName:@"GMT"]];

    [comps setTimeZone:[NSTimeZonetimeZoneWithName:@"GMT"]];

NSInteger unitFlags = NSEraCalendarUnit |

NSYearCalendarUnit |

NSMonthCalendarUnit |

NSDayCalendarUnit |

NSHourCalendarUnit |

NSMinuteCalendarUnit |

NSSecondCalendarUnit |

NSWeekCalendarUnit |

NSWeekdayCalendarUnit |

NSWeekdayOrdinalCalendarUnit |

NSQuarterCalendarUnit;

 

comps = [calendar components:unitFlags fromDate:selectDate];

int hour = [comps hour];

int min = [comps minute];

 

    NSString *tempTimeStr = [NSString stringWithFormat:@"%02d:%02d", hour, min];

//    self.timeStr = tempTimeStr;

    NSLog(@"time :%@", tempTimeStr);

    [self.recodeSwitchDic setObject:tempTimeStr forKey:kLotteryBetWarnTimeKey]; 

    [pool release];

}

//清除本地通知

- (void)cancelLocalNotification

{

    NSArray *localNotifications = [[UIApplicationsharedApplication] scheduledLocalNotifications];

    for(UILocalNotification *notification in localNotifications)

    {

        NSLog(@"old localNotification:%@", [notification fireDate]);

        [[UIApplicationsharedApplication] cancelLocalNotification:notification];

    }

}

//设置本地通知

 

- (void)setLocalNotificationWithWeek:(NSString*)localWeek withContent:(NSString*)content

{

    NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

    

    NSMutableDictionary* mutableDic = [[NSUserDefaults standardUserDefaults] objectForKey:kLotteryBetWarnKey];

    if(!mutableDic)

    {

        return;

    }

    NSArray *clockTimeArray = [[mutableDic objectForKey:kLotteryBetWarnTimeKey] componentsSeparatedByString:@":"];

NSDate *dateNow = [NSDate date];

NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];

    //    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    //    [comps setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSInteger unitFlags = NSEraCalendarUnit |

NSYearCalendarUnit |

NSMonthCalendarUnit |

NSDayCalendarUnit |

NSHourCalendarUnit |

NSMinuteCalendarUnit |

NSSecondCalendarUnit |

NSWeekCalendarUnit |

NSWeekdayCalendarUnit |

NSWeekdayOrdinalCalendarUnit |

NSQuarterCalendarUnit;

 

comps = [calendar components:unitFlags fromDate:dateNow];

[comps setHour:[[clockTimeArray objectAtIndex:0] intValue]];

[comps setMinute:[[clockTimeArray objectAtIndex:1] intValue]];

[comps setSecond:0];

 

//------------------------------------------------------------------------

    //    NSString* clockMode = @"一、五";

Byte weekday = [comps weekday];

NSArray *array = [localWeek componentsSeparatedByString:@""];

Byte i = 0;

Byte j = 0;

int days = 0;

inttemp = 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;

}

}

}

 

for (i = 0; i < count; i++) {

    temp = clockDays[i] - weekday;

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

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

 

UILocalNotification *newNotification = [[UILocalNotificationalloc] init];

if (newNotification) {

newNotification.fireDate = newFireDate;

newNotification.alertBody = content;//设置弹出对话框的消息内容

            if([[mutableDic objectForKey:kLotteryBetWarnSongKey] isEqualToString:@"1"])

                newNotification.soundName = UILocalNotificationDefaultSoundName;//设置声音

newNotification.alertAction = @"确定";//设置弹出对话框的按钮

newNotification.repeatInterval = NSWeekCalendarUnit;//周期为每星期

            newNotification.applicationIconBadgeNumber = 1;//设置BadgeNumber

            

NSDictionary *userInfo = [NSDictionarydictionaryWithObject:@"1"forKey:@"betLotteryWarnClock"];

newNotification.userInfo = userInfo;

[[UIApplicationsharedApplication] scheduleLocalNotification:newNotification];//记录

}

NSLog(@"Post new localNotification:%@", [newNotification fireDate]);

[newNotification release];

}

    [pool release];

}

 

posted @ 2013-01-05 14:30  燕羽天空  Views(629)  Comments(0Edit  收藏  举报