错误的开火日期UILocalNotification - 开火日期提前一小时

时间:2015-09-07 16:54:50

标签: ios nsdate uilocalnotification

我想将UILocalNotification设置为在某个时间关闭。该应用程序处理餐厅预订。如果预订完成且预约时间超过2小时,那么我想在2小时前显示通知。如果它大于1小时,那么我是在手前1小时展示它,否则我想在手前显示它15分钟。我所遇到的困难是我的UILocalNotification开火时间错误+ 1 hour。以下是使用的代码:

[self setLocalNotificationWithAlertBody:@"Alert Body goes here" AndWithBookingDateString:@"2015-09-07 19:45:00"];//the booking time



-(void)setLocalNotificationWithAlertBody:(NSString *)body AndWithBookingDateString:(NSString *)dateString{

    NSLog(@"Date String %@",dateString);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [[NSDate alloc] init];

    dateFromString = [dateFormatter dateFromString:dateString];
    NSLog(@"Date from String %@",[dateFormatter dateFromString:dateString]);

    [self setLocalNotificationWithAlertBody:body AndWithBookingDate:dateFromString];
}



 -(void)setLocalNotificationWithAlertBody: (NSString *) body AndWithBookingDate: (NSDate *)date{

    NSDate *currentDate = [NSDate date];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit
                                           fromDate:currentDate
                                             toDate:date
                                            options:0];


    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    NSInteger hour = [components hour];


    if (components.hour>=2) {//remind the user 2 hours before hand if booking is made greater than 2 hours


        NSTimeInterval secondsPerHour = (60 * 60)*2;//two hours before appointment
        NSDate *givenDate = date; // what you have already
        NSDate *earlierDate = [givenDate dateByAddingTimeInterval:-secondsPerHour];
         NSLog(@"\n\n Greater than 2 hours %@ \n\n",earlierDate);
        localNotification.fireDate=earlierDate;
        localNotification.alertBody = [NSString stringWithFormat:@"%@ %@",body,[self getDeviceShortDateFromString:[NSString stringWithFormat:@"%@",date] WithFormat:@"yyyy-MM-dd HH:mm:ss ZZZZZ"]];
    }
    else if (components.hour>1) {

        NSTimeInterval secondsPerHour = 60 * 60;
        NSDate *givenDate = date; // what you have already
        NSDate *earlierDate = [givenDate dateByAddingTimeInterval:-secondsPerHour];
        NSLog(@"\n\n Greater than an hour %@ \n\n",earlierDate);
        localNotification.fireDate=earlierDate;
        localNotification.alertBody = [NSString stringWithFormat:@"%@ %@",body,[self getDeviceShortDateFromString:[NSString stringWithFormat:@"%@",date] WithFormat:@"yyyy-MM-dd HH:mm:ss ZZZZZ"]];

    }
    else{


        NSTimeInterval secondsPer15mins = 15 * 60;
        NSDate *givenDate = date; // what you have already
        NSDate *earlierDate = [givenDate dateByAddingTimeInterval:-secondsPer15mins];
         NSLog(@"\n\n Less than one hour %@ \n",earlierDate);


        localNotification.fireDate=earlierDate;
        localNotification.alertBody = [NSString stringWithFormat:@"%@ %@",body,[self getDeviceShortDateFromString:[NSString stringWithFormat:@"%@",date] WithFormat:@"yyyy-MM-dd HH:mm:ss ZZZZZ"]];


    }


    localNotification.userInfo = @{@"key" : body};
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    NSLog(@"Fire date %@",localNotification.fireDate);

    NSLog(@"Notification--->: %@", localNotification);


}


  -(NSString *)getDeviceShortDateFromString: (NSString *) date WithFormat: (NSString *)format{


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:format];

    NSDate * tempDate =[dateFormatter dateFromString: date];

    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat:@"MMM yyyy"];
    if (tempDate == nil) {
        return @" ";
    }

    return [dateFormatter2 stringFromDate:tempDate];

}

开火日期记录为:

Fire date 2015-09-07 17:45:00 +0000

但是本地通知是错误的:

Notification--->: <UIConcreteLocalNotification: 0x174178300>{fire date = Monday 7 September 2015 18:45:00 Irish Summer Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday 7 September 2015 18:45:00 Irish Summer Time, user info = {
key = "Alert Body Goes here";
}}

约会预订时间超过2小时,因此通知应在5:45结束,但直到6:45才会开始。非常感谢任何帮助。

“帮我StackOverflow,你是我唯一的希望”:)

1 个答案:

答案 0 :(得分:0)

对于依赖于特定位置结果的可扩展应用,请确保您的位置特定属性始终指向用户的相对位置。在您的情况下,您只是忽略了一个简单的设置:

[NSTimeZone systemTimeZone]

对于你来说,这是+1小时,但是对于其他人来说,它会有所不同,有些人会落后,有些人会落后,因为你的基础是GMT的时间。

我只想将其改为{{1}}