2015-09-07 9 views
0

我想設置一個UILocalNotification在某個時間關閉。該應用程序處理餐廳預訂。如果預訂已經完成並且預約時間大於2小時,那麼我想在2小時前顯示通知。如果它大於1小時,那麼我會在1小時前展示它,否則我想在展示15分鐘前展示它。我的缺點是我的UILocalNotification的火災時間是錯誤的+ 1 hour。下面是使用的代碼:錯誤的啓動日期UILocalNotification - 啓動日期提前一小時

[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,你是我唯一的希望」 :)

+0

如果這是爲什麼不將它設置爲系統時區中的用戶自己的時區?如果我是使用該應用程序的用戶,並且從現在開始2個小時後安排預訂,那麼從現在開始的兩小時內,我的時區應該正確嗎?除非你試圖吃一個格林威治標準時區 – soulshined

+0

@soulshined被發現的人,謝謝你。 – DevC

回答

0

對於那些依靠位置具體結果可擴展的應用程序,讓您的位置特定屬性始終指向用戶的相對位置。在你的情況,你只是忽略了一個簡單的設置:

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; 

對你來說,這是+1小時,但對其他人來說可能會有所不同有的會提前,有的會落後,因爲你在格林尼治標準時間的基礎的fireDate時間。

我想簡單地改變這是[NSTimeZone systemTimeZone]

+0

感謝它的工作。 :) – DevC

相關問題