2012-12-08 17 views
2

我已經設置了一個本地通知,我的問題是我已經將它設置爲星期六和一週的重複間隔。但是,我設定了正確的日期,但我仍然在每個星期日都會得到它,但在正確設定的時間。有人看到我的錯誤嗎?哦,不要忘記,如果我設置了正確的一天,如果......星期三,我會在一分鐘後立即收到通知。不知道我的錯在哪裏。本地通知調度問題

- (void)applicationDidEnterBackground:(UIApplication *)applicatio 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ; 

    NSDateComponents *componentsForReferenceDate = 

    [calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]]; 

    //set day (saturday) 

    [componentsForReferenceDate setDay:1] ; 
    [componentsForReferenceDate setMonth:12] ; 
    [componentsForReferenceDate setYear:2012] ; 

    NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ; 

    // set components for time 18:30. 

    NSDateComponents *componentsForFireDate = 

    [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit) fromDate: referenceDate]; 

    [componentsForFireDate setHour: 18] ; 
    [componentsForFireDate setMinute:38] ; 
    [componentsForFireDate setSecond:0] ; 

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; 

    // Create the notification 

    UILocalNotification *notification = [[UILocalNotification alloc] init] ; 

    notification.fireDate = fireDateOfNotification ; 
    notification.timeZone = [NSTimeZone localTimeZone] ; 
    notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ; 
    notification.alertAction = @"Back"; 
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"]; 
    notification.repeatInterval= NSWeekCalendarUnit ; 
    notification.soundName = @"Appnotifisound.wav"; 
    notification.applicationIconBadgeNumber = 1; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; 

謝謝。

回答

0

這是因爲您正在創建兩個不同的NSDateComponents。一個設置日期,月份和年份,另一個設置小時,分鐘和秒。

這應該工作:

- (void)applicationDidEnterBackground:(UIApplication *)application { 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;  
    NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init]; 

    //set day (saturday) 

    [componentsForReferenceDate setDay:7] ; 
    [componentsForReferenceDate setMonth:12] ; 
    [componentsForReferenceDate setYear:2012] ; 

    // set time (08:30 PM) 

    [componentsForReferenceDate setHour: 20] ; 
    [componentsForReferenceDate setMinute:30] ; 
    [componentsForReferenceDate setSecond:00] ; 

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate]; 

    // Create the notification 

    UILocalNotification *notification = [[UILocalNotification alloc] init] ; 

    notification.fireDate = fireDateOfNotification ; 
    notification.timeZone = [NSTimeZone localTimeZone] ; 
    notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ; 
    notification.alertAction = @"Back"; 
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"]; 
    notification.repeatInterval= NSWeekCalendarUnit ; 
    notification.soundName = @"Appnotifisound.wav"; 
    notification.applicationIconBadgeNumber = 1; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; 

} 
+0

感謝你洙多。 –

+0

我很高興能幫上忙。 – Myxtic