2013-12-13 29 views
0

我的本地通知过早发射了大约2个小时。我将它设置为启动的日期显示 2013-12-13 17:00:00 +0000本地通知在错误时间触发

但是,我的电话在15:00:00向我发送了通知。我想让它在用户当地时间开火。我该如何调整日期,以便在用户的当地时区触发......即,不管你在美国的哪个地方,它在17:00:00点燃?

这是我的NSDate的旅程。我解析一个XML文件。从pubdate的标签:

NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822]; 
      NSString *articleImage = [item valueForChild:@"description"]; 
      NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
      [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
      [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 

      NSString *dateofarticle = [dateFormatter stringFromDate:articleDate]; 
      NSDate *date = [dateFormatter dateFromString:dateofarticle]; 

中的NSDate *日期被添加到财产我已经设置了存储一切从XML的每个项目的自定义类。所以,用于通知的NSDate就是这样。当我设置通知时,我不对NSDate进行调整。

从提醒UIActivity:

- (void)prepareWithActivityItems:(NSArray *)activityItems 
{NSLog(@"works"); 
    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls != nil) { 
     NSString *message = [@"5 minutes until " stringByAppendingString:self.thetitle]; 
     UILocalNotification *notif = [[cls alloc] init]; 
     // NSLog(@"%@", self.thedate); 
     NSDate *newDate = [self.thedate dateByAddingTimeInterval:-60*5]; 
     NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init]; 

     [formatter3 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
     [formatter3 setTimeStyle:NSDateFormatterShortStyle]; 
     [formatter3 setDateStyle:NSDateFormatterShortStyle]; 
     NSString *detailstext = [formatter3 stringFromDate:newDate]; 
     NSDate *othernewdate = [formatter3 dateFromString:detailstext]; 
     NSLog(@"other%@", othernewdate); 
     notif.timeZone = [NSTimeZone systemTimeZone]; 

     notif.fireDate = othernewdate; 

     //NSLog(@"newdate%@", newDate); 

     notif.alertBody = message; 
     notif.alertAction = @"Ok"; 
     notif.soundName = UILocalNotificationDefaultSoundName; 
     notif.applicationIconBadgeNumber = 1; 


       notif.repeatInterval = 0; 


     NSDictionary *userDict = [NSDictionary dictionaryWithObject:message 
                  forKey:kRemindMeNotificationDataKey]; 
     notif.userInfo = userDict; 

     [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
     [self.notifications addObject:notif]; 
     [notif release]; 

    } 
    NSLog(@"Test"); 
    [self activityDidFinish:YES]; 
    } 
- (void)showReminder:(NSString *)text { 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                 message:text delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 
} 

的的NSLog(@ “其他%@”,othernewdate);在控制台中显示为2013/12/13 16:55:00 +0000。我在中部时区。

UPDATE#2 在预定通知的区域,我正在运行一些测试。我将XML pubDate更改为在时间之后显示-0600,以便保持中央时间。我运行这个代码和日志。

NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init]; 

     [formatter3 setTimeStyle:NSDateFormatterShortStyle]; 
     [formatter3 setDateStyle:NSDateFormatterShortStyle]; 
     NSString *detailstext = [formatter3 stringFromDate:self.thedate]; 
     NSDate *othernewdate = [formatter3 dateFromString:detailstext]; 
     NSLog(@"details%@", detailstext); 
     NSLog(@"other%@", othernewdate); 

字符串显示了这一特定条目正确的日期和时间:

details12/27/13,3:00 PM

的NSDate的说明这一点。所以,我的问题是这样的。由于下面那个时候在技术上是同一时间......应通知消防正确?:

other2013-12-27 21:00:00 +0000

回答

3

确保有效的时区对象分配给本地通知,即:

localNotification.timeZone = [NSTimeZone systemTimeZone]; 

如果需要通知开火时区的17:00,用timeZoneWithName:创建您的时区的时区对象,并设置为本地通知的时区。

+0

无论您身在何处,我都希望它在下午5点开火。到下午5点时,通知会触发。但是,即使将时区设置为systemTimeZone也不适合我。 – user717452

+0

你是如何设定日期的?如果你在调试器中打印一个NSDate对象,它将在GMT时区显示,所以不要相信它在那里所说的内容。 –

+0

有关更多信息,请参阅上面的编辑,因为它太长而无法到达此处。 – user717452