[英]Local Notification issue iphone
我想发出每天通知用户5次并每天重复一次的本地通知,我将它们放置在一个可变数组中,该对象是“ hh:mm”,对于格林尼治标准时间+3城镇固定了小时和分钟,所以我得到了当前日期并找到间隔,然后为该通知创建一个日期,这就是我实现的方法。 -首先应用时区,-如果当前时间之前的时间为-秒,则将其设置为第二天。 -第三天设置该日期的本地通知。 请帮我
使用此示例代码,我将2条通知安排在早上7点,另一条安排在晚上6点,并每天重复一次,它的工作效果非常好,希望您能找到使用它的解决方案。
#pragma mark
#pragma mark - Notification Setup
-(void)clearNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:@"ParsingResponse"];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:7];
[components setMinute:0];
NSDate *today7am = [calendar dateFromComponents:components];
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = today7am;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatCalendar = [NSCalendar currentCalendar];
notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Morning"];
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Morning", @"key", nil];
notif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components2 setHour:18];
[components2 setMinute:0];
NSDate *today6pm = [calendar2 dateFromComponents:components2];
UILocalNotification *notif2 = [[cls alloc] init];
notif2.fireDate = today6pm;
notif2.timeZone = [NSTimeZone defaultTimeZone];
notif2.repeatCalendar = [NSCalendar currentCalendar];
notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Evening"];
notif2.alertAction = @"Show me";
notif2.soundName = UILocalNotificationDefaultSoundName;
notif2.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Evening", @"key", nil];
notif2.userInfo = infoDict2;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.