为特定 EST 时间设置本地通知,而不考虑用户的默认时区



我想为特定的EST时间设置本地通知,并且无论用户的默认时区如何,用户都应该获得通知。比方说,如果我想让本地通知在美国东部时间中午12点到达,那么太平洋时区的某个人也应该在美国东部时间中午12点也就是太平洋时间09:00时收到通知

这是我尝试做的,但是只有当我将默认时区设置为EST时才会收到通知

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.repeatInterval = kCFCalendarUnitDay;
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *today = [NSDate date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit |       NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:today];    
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:dateComponents.day];
[dateComps setMonth:dateComponents.month];
[dateComps setYear:dateComponents.year];
[dateComps setHour:12];
[dateComps setMinute:00];
[dateComps setSecond:00];
NSDate  *fireDate = [calendar dateFromComponents:dateComps];
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
localNotif.alertBody = @"Daily Notification";
localNotif.alertAction = NSLocalizedString(@"View", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Daily Notification" forKey:@"acme"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

谁能告诉我我哪里做错了

我发现了这里的问题。应该为日期而不是通知设置时区,所以我必须添加这行代码:

[dateComps setTimeZone:[NSTimeZone timeZoneWithName:@"US/Eastern"]];

,应该注释代码行:

localNotif.timeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];

相关内容

最新更新