Repeating an iOS local notification

來源:互聯網
上載者:User
文章目錄
  • Setting the repeat interval
  • Limitations
  • Example code

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.