2

我创建了多个 UI 本地通知,例如 100 个。我有一组 UILocalNotifications,它们被安排为:

 -(void) scheduleNotification{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [self.notifications removeAllObjects];
    for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY];
        NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY];
        if (birthday) {
            [formatter setDateFormat:@"MM/dd/yyyy"];
            [formatter setLocale:[NSLocale currentLocale]];
            [formatter setTimeZone:[NSTimeZone systemTimeZone]];
            NSDate *date = [formatter dateFromString:birthday];
            if (date == nil) {
                [formatter setDateFormat:@"MM/dd"];
                [formatter setLocale:[NSLocale currentLocale]];
                [formatter setTimeZone:[NSTimeZone systemTimeZone]];
                date = [formatter dateFromString:birthday];
            }
            NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];

            componentsEnd.year = [[NSDate date] year];
            date = [gregorianEnd dateFromComponents:componentsEnd];
            self.alarmTime = [date dateByAddingTimeInterval:self.mTimeInterval];
            localNotification.fireDate = _alarmTime;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.applicationIconBadgeNumber = 1;

            NSString *itemName = @"B'day Alert!!!";
            NSString *msgName = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];
            NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil];
            localNotification.userInfo = userDict;
            localNotification.soundName = self.soundName;
            localNotification.alertBody = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];


            [self.notifications addObject:localNotification];
            } 
        }
    }
}

我已将我的applicationDidEnterBackground代表实现为:

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

    UILocalNotification* notification;

    for (int i = 0; i< [self.settingVC.notifications count]; i++) {

        notification = [self.settingVC.notifications objectAtIndex:i];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

}

另外,在didReceiveLocalNotification委托中,我有这个:

    - (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSString *itemName = [notification.userInfo objectForKey:TitleKey];
    NSString *messageTitle = [notification.userInfo objectForKey:MessageKey];
    [self _showAlert:itemName withTitle:messageTitle];
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}

但问题是我没有得到想要的输出。任何帮助将不胜感激。我知道在一个应用程序中只能有有限数量的预定通知;系统保留最快触发的 64 条通知并丢弃其余通知。那么,我将如何处理那 100 个或更多的 UILocalNotification?

4

0 回答 0