【问题标题】:Local notification scheduling issue本地通知调度问题
【发布时间】:2012-12-08 09:31:11
【问题描述】:

我已经设置了本地通知,但我的问题是我已将其设置为星期六,并且重复间隔为一周。但是,我设置了正确的日期,但我仍然在每个星期天得到它,但在正确设置的时间。有人看到我的错误吗?哦,别忘了,如果我设置了正确的日期,如果它...星期三,我会在一分钟后立即收到通知。不知道我的错在哪里。

    - (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] ;

谢谢。

【问题讨论】:

    标签: iphone ios uilocalnotification


    【解决方案1】:

    这是因为您正在创建两个不同的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] ;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多