在错误的时间点燃本地通知

时间:2013-12-13 23:04:37

标签: ios nsdate uilocalnotification

我当地的通知太早了大约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 * date从我设置的自定义类添加到属性,以存储来自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(@“other%@”,othernewdate);在控制台中显示为2013/12/13 16:55:00 +0000。我在中央时区。

更新#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

1 个答案:

答案 0 :(得分:3)

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

localNotification.timeZone = [NSTimeZone systemTimeZone];

如果您需要在 时区17:00触发通知,请使用timeZoneWithName:创建时区的时区对象,并将其设置为本地通知&# 39; s时区。