我正在開發一個日曆應用程序,其中包含由生日組成的多個事件(例如500個事件)。我正在從Web服務下載事件。我想在生日的上午10點的特定時間向用戶提供每個生日的警報。我使用本地通知來安排警報,但由於iOS僅允許每個應用程序發出64個通知,並且我有多個生日,所以我陷入了僵局。一旦應用超出限制,我不知道如何安排更多通知。請建議我如何解決這個問題。下面是我的代碼安排的通知如何在通知限制爲64時在iOS中安排更多本地通知
- (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 *newCompoents = [[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);
[newCompoents setYear:[todayComponents year]];
}
else{
NSLog(@"%d, %d", day, month);
[newCompoents setYear:[todayComponents year]+1];
}
[newCompoents setHour:[timeComponents hour]];
[newCompoents setMinute:[timeComponents minute]];
NSDate *combDate = [calendar dateFromComponents:newCompoents];
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限制交叉安排更多的通知。
國際海事組織你可以重新安排LocalNotification當你打開你的應用程序,如檢查總計劃localNotifications計數,如果它小於64,重新安排那裏.. – Bala
,只是看看這個答案http://stackoverflow.com/a/7721445/1059705 – Bala
是的,我可以使用該方法計算通知的總數。當你打開你的應用時,你的意思是什麼。我是否需要檢查我的應用程序委託中的計數並安排更多通知。 – suhit