簡體   English   中英

錯誤時間觸發本地通知

[英]Local Notification Firing At Wrong Time

我的本地通知大約提前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);

該字符串顯示以下特定條目的正確日期和時間:

詳細信息13/12/27,下午3:00

NSDate顯示了這一點。 所以,我的問題是這個。 由於以下時間在技術上是同一時間...該通知應正確觸發嗎?:

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

確保為本地通知分配一個有效的時區對象,即:

localNotification.timeZone = [NSTimeZone systemTimeZone];

如果你需要通知解雇你的時區的17:00,使用timeZoneWithName:創建您的時區的時區對象,並設置為本地通知的時區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM