Handle multiple UILocalnotification more than 64
explain with code please! This question could be duplicated Managing multiple notifications UILocal But not getting an answer? Please focus on this question and anser How can I manage more than 64 notifications?
I have a local database of all UINotification
time, for example the following question:
Install multiple UILocalNotification
Answer, please.
source to share
It is not possible to schedule more than 64 local notifications at any given time. However, there may be ways to effectively achieve what you need.
- If you're repeating notifications, don't schedule them manually. Use
repeatInterval
for object insteadUILocalNotification
. This property takes on a valueNSCalendarUnit
that can be used to set the repeat as hourly, daily, monthly, etc. - Reevaluate the scheduled items each time you run the application
UILocalNotification
. Discard the ones that are no longer valid and queue up as many as possible that will be in the future. - This is the least elegant solution, but potentially the best for setting a lot of reminders for the user. However, please note, before attempting this method, you are not actually going to register additional objects
UILocalNotification
and therefore your user will not be sent to the application when the notification is displayed. Therefore, for this purpose, you can use calendar events and reminders to set reminders for the user. Would you like to use the EventKit frameworkfor handling all heavy lifting. The advantage of this method is that you plan to schedule as many events as you like. And you can use the calendar as a backing store for your notifications. Therefore, every time the application starts, you queryEKEventStore
using one of the predicate methods (for example- (NSArray *)eventsMatchingPredicate:(NSPredicate *)predicate
) to find the events you created.
source to share
There may be other ways to control it UILocalNotification
. According to apple, you can set a maximum of 64 notifications. so for managing more than 64 notifications you can implement below logic.
First, register the first 64 Local Notifications. every time you start the app or didBecomeActive get the remaining notifications or counting with help [[UIApplication sharedApplication]scheduledLocalNotifications]
. so if your counter is <64 then say it 30. so add new 34 notifications and now your total notification will be 64. do this every run to make it work according to your needs.
Maybe this will help you.
source to share
Try it, you can change the interval by setDay
, setMonth
....:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:3];
NSDate *date3Days = [calendar dateByAddingComponents:components
toDate:[NSDate date]
options:0];
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
if ( oldNotifications ) {
[app cancelAllLocalNotifications];
app.applicationIconBadgeNumber = 0;
}
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
notifyAlarm.fireDate = date3Days;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.alertBody = NSLocalizedString( @"Push message", @"");
notifyAlarm.soundName = @"sound.wav";
[app scheduleLocalNotification:notifyAlarm];
}
If you want to set a specific time after which it should appear UILocalNotifications
, you can create a method of the above solution and loop through the array that indicates the days you like to show the notification:
NSArray *arrayNumbers = @[@5, @7, @14, @21, @30, @60, @90, @120];
NSDictionary *dictNotifications =
[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefAppStarts];
for ( NSNumber *bla in arrayNumbers ){
NSString *strKey = [NSString stringWithFormat:@"%@%@", kUserDefAppStarts, bla];
NSDictionary *dict = dictNotifications[strKey];
NSString *strMessageQuestion = dict[kKeyMessage];
[self createNotificationWithNumberOfDays:[bla integerValue]
andMessage:strMessageQuestion
userInfo:dict];
}
And here is the method you should call
+ (void)createNotificationWithNumberOfDays:(int)days
andMessage:(NSString *)message
userInfo:(NSDictionary *)dict{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
if( notifyAlarm ){
[notifyAlarm setFireDate:dateAlert];
[notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
[notifyAlarm setSoundName:@"soundname"];
[notifyAlarm setAlertBody:message];
[notifyAlarm setUserInfo:dict];
[app scheduleLocalNotification:notifyAlarm];
}
}
source to share
Since iOS7 you can use background app refresh to schedule local notifications as well. On every update call, you can check your database for upcoming (future) notifications and schedule them just like in your app. We've used this approach in one of our apps and Apple doesn't seem to have any problems with it. Also note that the user can turn it off, so you have to convince your user that you will not drain the battery :)
You can get more information from the documentation: What's New in iOS7
and here: application: execute the FetchWithCompletionHandler:
source to share