如何在iOS中安排更多的本地通知,当通知超过64个限制时

8
我正在开发一个日历应用程序,其中包含多个事件(例如500个事件),其中包括生日。我正在从Web服务下载事件。我想在每个生日的特定时间,例如早上10点,为用户提供通知。我正在使用本地通知进行警报调度,但是我遇到了问题,因为iOS每个应用程序只允许64个通知,并且我有多个生日。一旦应用程序超过限制,我不知道如何安排更多的通知。请建议我如何解决这个问题。以下是我的代码来安排通知。
- (void)scheduleNotification:(NSMutableArray *)datesArray withMessage:(NSMutableArray *)messagesArray  NotificationID:(NSString *)notificationID
{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];


    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    [formatter1 setDateStyle:NSDateFormatterMediumStyle];
    [formatter1 setDateFormat:@"dd/MM/yyyy"];

   // NSDate *myTime = [formatter dateFromString:@"06:10 PM"];
    NSDate *myTime = [formatter dateFromString:fireTime];

    for (int i = 0; i < [datesArray count]; i++)
    {
        NSDate *myDate = [formatter1 dateFromString:[datesArray objectAtIndex:i]];

        NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:myDate];
        NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myTime];

        NSDateComponents * newComponents = [[NSDateComponents alloc]init];
        NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

        int day = [dateComponents day];
        int month = [dateComponents month];

        [newCompoents setDay:[dateComponents day]];
        [newCompoents setMonth:[dateComponents month]];

        if (day >= [todayComponents day] && month >= [todayComponents month]) { 
            NSLog(@"day = %d, month = %d", day, month);
            [newComponents setYear:[todayComponents year]];
        } else {
            NSLog(@"%d, %d", day, month);
            [newComponents setYear:[todayComponents year]+1];
        }


        [newComponents setHour:[timeComponents hour]];
        [newComponents setMinute:[timeComponents minute]];

        NSDate *combDate = [calendar dateFromComponents: newComponents];

        localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        localNotif.fireDate = combDate;
        localNotif.timeZone = [NSTimeZone defaultTimeZone];

        // Notification details

        NSString *message = [@"Wish" stringByAppendingFormat:@" %@%@", [messagesArray objectAtIndex:i],@" On His Birthday"];
        localNotif.alertBody = message;

        // Set the action button
        localNotif.alertAction = @"View";

        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;

        // Specify custom data for the notification
        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
        localNotif.userInfo = infoDict;

        // Schedule the notification
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }

}

如果我有任何错误,请纠正并建议如何安排超过64个通知的更多通知。


在我看来,你可以在每次打开应用程序时重新安排本地通知,例如检查总计划的本地通知数量,如果少于64,则在那里重新安排。 - Bala
请看这个答案:https://dev59.com/nmsz5IYBdhLWcg3wsaAN#7721445 - Bala
是的,我可以使用那个方法来计算通知的总数。你说的“每当打开应用程序”是什么意思?我需要在我的应用程序委托中检查计数并安排更多通知吗? - Suhit Patil
在 didReceiveLocalNotification: 和 didLaunchApplication 中 - Bala
1个回答

1
我有一个类似的问题,并且还有一些额外的选项可以帮助你,尽管它们都有缺陷,这意味着无法保证正确的行为。
请记住,重叠的生日可能在这里很有用。如果两个人最终在同一天过生日,请取消并重新安排与他们两个人的信息相关联。如果您可以接受通用消息,您可以找到其他重复出现的时间间隔。
显然,鼓励用户按下通知以打开应用程序会有所帮助(可能还涉及您的广告收入)。您可以尝试一个好的消息作为最后一条。
虽然有点hacky,但您可以使用地理围栏(但可能不是远程通知),如此处所述:Can push notifications be used to run code without notifying user?。如果您添加了使用它的功能,甚至可能能够获得批准。
我也考虑传递自定义日历,但NSCalendar是免费桥接到CFCalendarRef,而且似乎我试图深入研究它(并且至少得到批准)的运气不太好。我很高兴被告知我错了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接