<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; uilocalnotification</title>
	<atom:link href="/tag/uilocalnotification/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Fri, 19 Oct 2012 15:01:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>iPhone Programming Tutorial &#8211; Local Notifications</title>
		<link>http://icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/</link>
		<comments>http://icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 19:26:53 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[alert]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[uilocalnotification]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2165</guid>
		<description><![CDATA[Way back when, when everyone was still complaining about Apple's lack of support for (3rd party) multitasking, there was a simple solution put in place. This solution was known as push notifications.

Push notifications solved many of the issues associated with background processing.  For example, when quitting the AIM application, the server could keep you logged in and send you a push notification when a new message arrived.  You could then tap on a View button that would launch the app.

This solution is great and all, but it still requires that you have an active internet connection.  As of iOS4, Apple has introduced a new type of notification that can be scheduled to fire within the device itself.  It requires no complicated server programming, or additional configuration with iTunes.  I am talking about Local Notifications.]]></description>
			<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.31.32-AM1.png"><img class="size-full wp-image-2181 alignright" title="Screen shot 2010-07-29 at 11.31.32 AM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.31.32-AM1.png" alt="" width="289" height="398" /></a>Way back when, when everyone was still complaining about Apple&#8217;s lack of support for (3rd party) multitasking, there was a simple solution put in place. This solution was known as push notifications.</p>
<p>Push notifications solved many of the issues associated with background processing.  For example, when quitting the AIM application, the server could keep you logged in and send you a push notification when a new message arrived.  You could then tap on a View button that would launch the app.</p>
<p>This solution is great and all, but it still requires that you have an active internet connection.  As of iOS4, Apple has introduced a new type of notification that can be scheduled to fire within the device itself.  It requires no complicated server programming, or additional configuration with iTunes.  I am talking about <strong>Local Notifications</strong>.</p>
<p>Local notifications can be scheduled on the user&#8217;s iDevice to fire at any given time; you can even set them to be recurring.  Today, we will explore these notifications and I will provide you with a simple example of how to schedule, view, and handle local notifications.  Here is a quick screenshot of the project that we are going to create (note I am using the iPhone 4  simulator).</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.28.46-AM1.png"><img class="alignnone size-full wp-image-2168" title="Screen shot 2010-07-29 at 11.28.46 AM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.28.46-AM1.png" alt="" width="482" height="664" /></a></p>
<p style="text-align: left;">The project will allow a user to schedule a location notification to fire off at a given date.  Using the text field, they are also able to specify some text for the notification.  The table view below displays a list of all of the currently scheduled location notifications within the application.</p>
<p style="text-align: left;">So, by now (if you are an avid iCodeBlog reader), you are already a killer iPhone dev and I can rush through the stuff that is not related to the notifications.  I will try to provide links to tutorials about sections that I rush through as well.</p>
<h2>1. Create a View-Based Application</h2>
<p style="text-align: left;">We will be using this as our starting point.  Check out <a href="/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/">this tutorial</a> if you are unfamiliar with doing this.  Name the project <strong>Notifier</strong>.</p>
<h2>2. Create All Properties and Dummy IBActions</h2>
<p style="text-align: left;">This is usually a good first step when tackling an application like this.  Let&#8217;s get everything set up in the .h and .m files so we only have to visit Interface Builder Once.  Here is what our NotifierViewController.h file looks like.</p>
<p style="text-align: left;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> NotifierViewController <span style="color: #002200;">:</span> UIViewController<span style="color: #002200;">&amp;</span>lt;UITableViewDelegate,UITableViewDataSource<span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#123;</span>
	IBOutlet UITableView <span style="color: #002200;">*</span>tableview;
	IBOutlet UIDatePicker <span style="color: #002200;">*</span>datePicker;
	IBOutlet UITextField <span style="color: #002200;">*</span>eventText;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITableView <span style="color: #002200;">*</span>tableview;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIDatePicker <span style="color: #002200;">*</span>datePicker;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextField <span style="color: #002200;">*</span>eventText;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> scheduleAlarm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p style="text-align: left;">Seems clear enough.  We have 3 UI elements that we care about and one action.  One thing to note is, your class should implement the UITableViewDelegate and UITableViewDataSource protocols.  This is because we will be displaying a tableview containing all of the scheduled alarms.</p>
<p style="text-align: left;">Now, do all of the necessary steps in your .m file.  This includes memory management for the IBOutlets and creating a dummy method for the scheduleAlarm IBAction.  Your .m file should look something like this.  Note: I have omitted import statements because my syntax highlighter wasn&#8217;t digging them.</p>
<p style="text-align: left;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> NotifierViewController
&nbsp;
<span style="color: #a61390;">@synthesize</span> datePicker,tableview, eventText;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> scheduleAlarm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>didReceiveMemoryWarning <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super didReceiveMemoryWarning<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidUnload <span style="color: #002200;">&#123;</span>
	datePicker <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	tableview <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	eventText <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p style="text-align: left;">Now it&#8217;s time to build our interface.  Open Interface builder and construct an interface like this.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-10.52.35-AM1.png"><img class="alignnone size-full wp-image-2174" title="Screen shot 2010-07-29 at 10.52.35 AM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-10.52.35-AM1.png" alt="" width="400" height="582" /></a></p>
<p style="text-align: left;">If you want my super sweet green button image, here it is:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/button-green1.png"><img class="size-full wp-image-2175 aligncenter" title="button-green" src="/wp-content/uploads/2010/07/button-green1.png" alt="" width="292" height="47" /></a></p>
<p style="text-align: left;">After creating the interface, make sure you hook up all of the UI components to their corresponding IBOutlets and hook up the touchUpInside: method of the button the your scheduleAlarm: IBAction.  For more info on hooking up IBOutlets, check out <a href="/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/">this tutorial</a>.</p>
<h2>3. Implement UITableViewDelegate and UITableViewDataSource Delegate methods to List Currently Scheduled Local Notifications</h2>
<p style="text-align: left;">It may seem weird to implement the code to display the notifications before the code that creates them, however I like this approach better.  This way, once we schedule the notifications, they automagically appear in our table.  Add the following code to your .m file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// We only have one section</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Return the number of notifications</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduledLocalNotifications<span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleSubtitle reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get list of local notifications</span>
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>notificationArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduledLocalNotifications<span style="color: #002200;">&#93;</span>;
    UILocalNotification <span style="color: #002200;">*</span>notif <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>notificationArray objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Display notification info</span>
    <span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span>notif.alertBody<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>cell.detailTextLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>notif.fireDate description<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: left;">OK, finally some &#8220;real&#8221; code.  Most of this code should seem pretty straight forward.  If not, <a href="/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/">check out this tutorial</a> on UITableViews.</p>
<p style="text-align: left;">So, the new code here is dealing with retrieving a list of scheduled notifications.  Calling the scheduledLocalNotifications method of UIApplication will return an NSArray of all notifications scheduled by the current app.  We just index into this array and grab each notification.</p>
<p style="text-align: left;">Finally, we are displaying the alertBody (text that displays when the notification fires) and the fireDate (date and time when the notification will display) in the tableview cell.</p>
<h2>4. Scheduling Notifications</h2>
<p>And now for the moment you&#8217;ve been waiting for&#8230; OK, probably not, but definitely the most exciting (least boring) part of this tutorial.  Let&#8217;s implement that scheduleAlarm: IBAction that you framed out earlier.  Update your .m file to contain the following code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> scheduleAlarm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>eventText resignFirstResponder<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSCalendar</span> <span style="color: #002200;">*</span>calendar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCalendar</span> autoupdatingCurrentCalendar<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get the current date</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>pickerDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.datePicker date<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Break the date up into components</span>
    <span style="color: #400080;">NSDateComponents</span> <span style="color: #002200;">*</span>dateComponents <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>calendar components<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span> NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit <span style="color: #002200;">&#41;</span>
												   fromDate<span style="color: #002200;">:</span>pickerDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDateComponents</span> <span style="color: #002200;">*</span>timeComponents <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>calendar components<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span> NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit <span style="color: #002200;">&#41;</span>
												   fromDate<span style="color: #002200;">:</span>pickerDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Set up the fire time</span>
    <span style="color: #400080;">NSDateComponents</span> <span style="color: #002200;">*</span>dateComps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateComponents</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setDay<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dateComponents day<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setMonth<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dateComponents month<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setYear<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dateComponents year<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setHour<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>timeComponents hour<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// Notification will fire in one minute</span>
    <span style="color: #002200;">&#91;</span>dateComps setMinute<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>timeComponents minute<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>dateComps setSecond<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>timeComponents second<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>itemDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>calendar dateFromComponents<span style="color: #002200;">:</span>dateComps<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps release<span style="color: #002200;">&#93;</span>;
&nbsp;
    UILocalNotification <span style="color: #002200;">*</span>localNotif <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILocalNotification alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>localNotif <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
    localNotif.fireDate <span style="color: #002200;">=</span> itemDate;
    localNotif.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> defaultTimeZone<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Notification details</span>
    localNotif.alertBody <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventText text<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// Set the action button</span>
    localNotif.alertAction <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;View&quot;</span>;
&nbsp;
    localNotif.soundName <span style="color: #002200;">=</span> UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Specify custom data for the notification</span>
    <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>infoDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someValue&quot;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someKey&quot;</span><span style="color: #002200;">&#93;</span>;
    localNotif.userInfo <span style="color: #002200;">=</span> infoDict;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Schedule the notification</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduleLocalNotification<span style="color: #002200;">:</span>localNotif<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localNotif release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self.tableview reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>So, most of the explanation is in the comments.  I&#8217;ll talk you through some of the less obvious stuff.  The first tricky area is dealing with the NSCalendar.  We just use the NSCalendar object to break up the date into components.  <strong>Note</strong>: This demo does not require that we break the date up into components.  You could have just as easily fed the date from the date picker into the notification fireDate.  The reason that I&#8217;m showing you how to break it down is, you may have some sort of custom date logic to work with and this makes things much easier in the future.</p>
<p>Another important bit of code is where we set the alertBody or the notification.  In this example we set it to the text that the user entered into the text field.  You can set this to whatever you like.</p>
<p>The other thing I want to mention is the infoDict in the code.  This dictionary is your chance to associate some additional information with the alert.  For example, if you are using this alert in a game like We Rule to notify you when a crop is ready.  You might want to set a key and value that contains the id of the crop that has completed.  For now, we just set some arbitrary values and you can ignore them if you like.</p>
<p>After actually scheduling the notification, we just reload the tableview to get it to display immediately.</p>
<h2>5. Handling Notifications After They Fire</h2>
<p>The last piece of this puzzle is determining what to do when a notification fires.  Fortunately, this step is very easy and handled inside of the appDelegate.  When a notification fires, there are one of two situations. 1. The app is running and 2. The app is not running (or running in the &#8220;background&#8221;) .</p>
<p>Open up your app delegate .m file and add the following code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Add the view controller's view to the window and display.</span>
    <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>viewController.view<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    application.applicationIconBadgeNumber <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Handle launching from a notification</span>
    UILocalNotification <span style="color: #002200;">*</span>localNotif <span style="color: #002200;">=</span>
    <span style="color: #002200;">&#91;</span>launchOptions objectForKey<span style="color: #002200;">:</span>UIApplicationLaunchOptionsLocalNotificationKey<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>localNotif<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Recieved Notification %@&quot;</span>,localNotif<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>app didReceiveLocalNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UILocalNotification <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Handle the notificaton when the app is running</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Recieved Notification %@&quot;</span>,notif<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The first thing we see here is the application badge number is getting set to 0.  Whenever a notification fires, it will increase the badge count on the application.  Next, we handle the case when the application launches from a notification.   This happens when the user presses the view button on the notification.  For now, we just NSLog the data, but you should handle the notification how you see fit for your app.</p>
<p>Finally, we implement the didReceiveLocalNotification method.  This method is required if you want to handle notifications at all in your app.  You will see this method fire when the app is running and you receive a local notification.  When the app is running, you will not see the UIAlertView show up with the notification data.</p>
<p>And there you have it!  The complete lifecycle of a local notification.  You may download the source for this tutorial below.  If you have any questions, feel free to post them in the comments section or <a href="http://twitter.com/brandontreb">write them to me on Twitter</a>.</p>
<p><a href="/wp-content/uploads/2010/07/Notifier1.zip">Notifier Source Code</a></p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/feed/</wfw:commentRss>
		<slash:comments>74</slash:comments>
		</item>
	</channel>
</rss>
