0

我为我的应用程序创建了一个区域设置通知,它每天都会触发,在苹果开发者文档中也提到你只能触发 64 个通知,所以我的问题是如何防止这种限制?我的意思是我的通知计划每年触发一次,那么这是取消通知然后按预定计划再次触发的正确方法吗?

- (void)cancelLocalNotification:(UILocalNotification *)notification {

[[UIApplication sharedApplication] cancelLocalNotification:notification;

}

这是我的通知代码:

- (void) notification  {


    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSPersianCalendar];
    NSDate *now = [NSDate date];    
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: now];

    [componentsForFireDate year];
    [componentsForFireDate month];
    [componentsForFireDate day];
    [componentsForFireDate setHour:1];
    [componentsForFireDate setMinute:2];
    [componentsForFireDate setSecond:1];

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.fireDate = fireDateOfNotification;
    notification.timeZone = [NSTimeZone localTimeZone]; 
    notification.repeatInterval= NSDayCalendarUnit; 


    notification.alertAction = @"View";
    notification.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}
4

1 回答 1

0

在这里阅读您的问题。它肯定解决了你的问题重复 iOS 本地通知

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
        NSDate *pickerDate = [self.datePicker date];
        NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                       fromDate:pickerDate];
        NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                       fromDate:pickerDate];
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setDay:[dateComponents day]];
        [dateComps setMonth:[dateComponents month]];
        [dateComps setYear:[dateComponents year]];
        [dateComps setHour:[timeComponents hour]];
        [dateComps setMinute:[timeComponents minute]];
        [dateComps setSecond:[timeComponents second]];
        NSDate *itemDate = [calendar dateFromComponents:dateComps];

        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        localNotif.fireDate = itemDate;
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        localNotif.alertBody = [eventText text];
        localNotif.alertAction = @"View";
        localNotif.soundName = audios;
        localNotif.applicationIconBadgeNumber = 1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        [localNotif release];
于 2012-04-17T06:38:38.490 回答