2011-12-07 30 views
1

我想爲特定的EST時間設置本地通知,並且用戶應該獲得通知,而不管用戶的默認時區。可以說,如果我希望當地通知在美國東部時間中午12點到達,那麼太平洋時區中的某個人也應該在中午12點正好是09:00 PST時收到通知。爲特定的EST時間設置本地通知,而不考慮用戶的默認時區

這是什麼我試圖做的,但我只得到通知,當我設置我的默認時區到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]; 

有人可以告訴我,我該怎麼錯在何處。

回答

1

我發現這裏的問題..時區應的日期,而不是對通知進行設置,所以我不得不添加此行代碼:

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

,並應註釋代碼line:

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