我正在开发一个包含多个事件(例如 500 个事件)的日历应用程序,其中包含生日。我正在从 Web 服务下载事件。我想在生日当天上午 10:00 的特定时间向用户提供每个生日的提醒。我正在使用本地通知来安排警报,但我被困住了,因为 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 个限制后安排更多通知。