开发者

Schedule number of Local Notifications

开发者 https://www.devze.com 2023-04-12 07:18 出处:网络
I am making an iPhone application in which I have implemented the concept of local notification to alert the user for taking medicine.

I am making an iPhone application in which I have implemented the concept of local notification to alert the user for taking medicine.

But in iO开发者_开发问答S we can't schedule more than 64 notifications at a time. But I have a number of date-time entries in the database. How could I schedule more than 64 notifications?


As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.

One way to make sure all notifications get scheduled is to schedule the first 64 notifications first, and then on regular time intervals (may be on every launch of the app or each time a notification fires) check for the number of notifications scheduled and if there are less than 64 notifications, lets say n notifications, then schedule the next (64 - n) notifications.

int n = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
int x = 64 - n;
// Schedule the next 'x' notifications


I've created app with local notifications and in this case user was notified every 20/40/60 minutes in given time interval (eg. 8:00-20:00). The solution was to generate up to 64 notification through whole day and set repeatInterval of UILocalNotification to NSCalendarUnitDay. Of course if you need to use more sophisticated notification pattern, this is not the way to go.

Also It's worth to mention, that since iOS7 I had to, due to client requirements, implement rescheduling of notifications in background app refresh.


Basic solution to this is using repeatInterval for notifications occurring at specific intervals. This will reduce the number of redundant notifications. But still if the number exceeds 64 then you can employ one strategy to use userInfo dictionary and pass the next notification's time and message in it and schedule it when the - (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification method is called. This can be a bit tricky so you've to be careful that proper data is passed in the userInfo dictionary and notifications are set accurately. I hope this helps.


I don't know of any other way than to schedule the first 64 notifications first, then at the launching, backgrounding and launching of a notifications, check if the number of notifications is least than 64 if it is then reschedule your notifications.

I don't get why Apple implementation of notifications is sooo lame. If anyone can figure a better way let me know.


I found this Swift library which is doing almost the same thing that we are looking for. I am going to try this and will confirm how it works:

https://github.com/ahmedabadie/ABNScheduler


Apple Doc says:

An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号