Repeating an iOS local notification
when I wrote about local notifications one thing that I left out was the ability to schedule a repeating notification. One of the reasons I did not bother to mention the ability to set a repeat interval is that the function is very limited. At least it is with iOS 4.1 at the time of writing. To show what can and can not be done I will modify the original sample app to include the ability to set a repeat interval.
Setting the repeat interval
The modified UI for the sample app now looks as below. A segmented control has been added to allow a repeat schedule to specified. The possible options are to repeat every minute, hourly, daily or weekly. If you think this is a limited set of options you will see that this not only down to my limited UI design skills but also because the possible options supported by local notifications are limited.
Other than modifying the XIB file the code changes to implement the repeat interval are minor. The method that creates the local notification (scheduleNotification) has the following additional lines:
NSInteger index = [scheduleControl selectedSegmentIndex];
switch (index) {
case 1:
notif.repeatInterval = NSMinuteCalendarUnit;
break;
case 2:
notif.repeatInterval = NSHourCalendarUnit;
break;
case 3:
notif.repeatInterval = NSDayCalendarUnit;
break;
case 4:
notif.repeatInterval = NSWeekCalendarUnit;
break;
default:
notif.repeatInterval = 0;
break;
}
The local notification object, which is of type UILocalNotification has a property named repeatInterval which determines the repeat interval for the delivery of the notification. The Apple documentation does not really provide much in the way of explanation or example but the repeatInterval is of type NSCalendarUnit which if you check the definition can take one of the following values:
- NSEraCalendarUnit
- NSYearCalendarUnit
- NSMonthCalendarUnit
- NSDayCalendarUnit
- NSHourCalendarUnit
- NSMinuteCalendarUnit
- NSSecondCalendarUnit
- NSWeekCalendarUnit
- NSWeekdayCalendarUnit
- NSWeekdayOrdinalCalendarUnit
- NSQuarterCalendarUnit
These are mostly self explanatory. If you set the repeatInterval to NSDayCalendarUnit then the notification will repeat every day at the same time as the initial notification. The definition of intervals such as week, month, quarter, etc. is actually dependent on the calendar used. You can specify a different calendar from the current calendar by setting the repeatCalendar property to an alternate NSCalendar object. In practise I am not sure you will ever find a practical reason to set an alternate calendar.
Limitations
The main limitation of notifications is that the user interface does not provide any way to cancel a repeating notification. So if you create a local notification with an hourly repeat interval it will repeat every hour until the user launches your app and gives you the chance to cancel it programatically. Note that the notification repeats even if the user selects the close/cancel button in the notification alert dialog.
The other obvious limitation is that you can only use one of the NSCalendarUnit repeat intervals. So you can have a repeat interval of one minute or one hour or one day but not five minutes or three hours or two days.
All things considered the ability to set a repeat interval for a local notification is of very limited use as currently implemented. In general I would say the handling of notifications is an area where Apple could make substantial improvements in a future release of iOS. Enhancing the UI to allow the user to view multiple notifications and snooze or cancel a repeating notification would make the feature much more useful. It is interesting to note that the Apple clock app has the ability to snooze an alarm but there is no obvious way to create a similar application with the current version of the public iOS frameworks.
Example code
Not much has changed but you can get the updated Xcode project for the example app here.
http://useyourloaf.com/blog/2010/9/13/repeating-an-ios-local-notification.html