我想每天两次发出本地通知,例如:每天早上7点,每天早上6点,所以有人可以帮助我,我该怎么做?
无法设置自定义时间点火本地通知已查看每个地方但没有任何帮助,一天两次火灾本地通知,如果有任何帮助将欣赏
提前致谢:)
这是我使用本地通知的代码的和平,但它根本没有解雇:(
- (void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
NSString *end = @"2013-09-20 11:24:00 +0000";
NSDate *endDate = [self convertStringToDate:end];
NSLog(@"end date :%@", endDate);
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = endDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
答案 0 :(得分:2)
问题在于您创建日期的方式:
NSString *end = @"2013-09-20 11:24:00 +0000";
NSDate *endDate = [self convertStringToDate:end];
NSLog(@"end date :%@", endDate);
您可以在11:24 UTC/GMT
创建日期,但是告诉UILocalNotification
使用系统时区。 Zo日期偏移到系统时区。
只需将timeZone
的UILocalNotification
设置为GMT
:
notif.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
然后UILocalNotification
将被设置为系统的正确时区,并且不会使用偏移量。
另一个选项是不在日期字符串中包含时区,并将NSDateFormatter
设置为包含系统时区。
但是使用上面的代码,即使时区发生变化也应该触发。
将repeatInterval
的UILocalNotification
设置为NSDayCalendarUnit
,使其每天重复。
notif.repeatInterval = NSDayCalendarUnit;
答案 1 :(得分:1)
你需要设置两个通知,一个用于早上,一个用于晚上;两者都将repeatInteval
属性设置为NSDayCalendarUnit
。
答案 2 :(得分:1)
最后得到解决方案如何设置UILocalNotification
上午7点和下午6点并每天重复它希望它有助于谁在通知上寻找相同的解决方案
-(void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
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 = @"Did you forget something?";
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[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 = @"Did you forget something2?";
notif2.alertAction = @"Show me2";
notif2.soundName = UILocalNotificationDefaultSoundName;
notif2.applicationIconBadgeNumber = 1;
notif2.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];
}
}