简体   繁体   English

错误时间触发本地通知

[英]Local Notification Firing At Wrong Time

My local notification is firing about 2 hours too early. 我的本地通知大约提前2个小时触发。 The date that I set it to fire at shows 2013-12-13 17:00:00 +0000 我将其设置为在的日期显示2013-12-13 17:00:00 +0000

However, my phone shot me the notification at 15:00:00. 但是,我的手机在15:00:00向我发送了通知。 I want it to fire at the users' local time. 我希望它在用户的本地时间触发。 How can I adjust the date so that it fires at the users' local time zone...ie no matter where in the USA you are, it fires at 17:00:00? 我如何调整日期,使其在用户的本地时区触发...即无论您在美国的哪个地方,它的触发时间都是17:00:00?

Here is the journey of my NSDate. 这是我的NSDate的旅程。 I parse an XML file. 我解析一个XML文件。 From the pubDate tag: 从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];

the NSDate *date gets added to the property from a custom class I have set up to store everything from each item of the XML. NSDate * date从我设置的自定义类添加到属性,该自定义类用于存储XML的每个项目的所有内容。 So, the NSDate that is getting used for the notification is that. 因此,用于通知的NSDate就是这样。 When I set the notification, I make no adjustments to the NSDate. 设置通知时,我不会对NSDate进行任何调整。

From reminder UIActivity: 从提醒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];
}

The NSLog(@"other%@", othernewdate); NSLog(@“ other%@”,othernewdate); shows as 2013/12/13 16:55:00 +0000 in the console. 在控制台中显示为2013/12/13 16:55:00 +0000。 I am in Central time zone. 我在中央时区。

UPDATE #2 In the area where a notification is scheduled I am running some tests. 更新#2在计划通知的区域中,我正在运行一些测试。 I changed the XML pubDate to show -0600 after the time so it would stay Central time. 我将XML pubDate更改为在时间之后显示-0600,因此它将保持中部时间。 I run this code and logs. 我运行此代码并记录日志。

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);

The string shows the correct date and time on this particular entry of: 该字符串显示以下特定条目的正确日期和时间:

details12/27/13, 3:00 PM 详细信息13/12/27,下午3:00

The NSDate shows this. NSDate显示了这一点。 So, my question is this. 所以,我的问题是这个。 Since that time below is technically the same time...should the notification fire correctly?: 由于以下时间在技术上是同一时间...该通知应正确触发吗?:

other2013-12-27 21:00:00 +0000 其他2013-12-27 21:00:00 +0000

Make sure to assign a valid timezone object to the local notification, ie: 确保为本地通知分配一个有效的时区对象,即:

localNotification.timeZone = [NSTimeZone systemTimeZone];

If you need the notifications to fire at your timezone's 17:00, use timeZoneWithName: to create a timezone object of your timezone and set that as the local notification's timezone. 如果你需要通知解雇你的时区的17:00,使用timeZoneWithName:创建您的时区的时区对象,并设置为本地通知的时区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM