简体   繁体   中英

Local Notification Firing At Wrong Time

My local notification is firing about 2 hours too early. The date that I set it to fire at shows 2013-12-13 17:00:00 +0000

However, my phone shot me the notification at 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?

Here is the journey of my NSDate. I parse an XML file. From the pubDate tag:

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. So, the NSDate that is getting used for the notification is that. When I set the notification, I make no adjustments to the NSDate.

From reminder 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); shows as 2013/12/13 16:55:00 +0000 in the console. I am in Central time zone.

UPDATE #2 In the area where a notification is scheduled I am running some tests. I changed the XML pubDate to show -0600 after the time so it would stay Central time. 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

The NSDate shows this. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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