Professional Documents
Culture Documents
together. The app lets users put in any to-do items and preset a reminder. At a
specific time, the app fires up a notification and reminds users about the to-do
item.
[self.itemText resignFirstResponder];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication
1
1
2
1
sharedApplication] applicationIconBadgeNumber] + 1;
3
1
[[UIApplication
4
sharedApplication] scheduleLocalNotification:localNotification];
1
5
// Request to reload table view data
1
6
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" obj
1
ect:self];
7
1
// Dismiss the view controller
8
[self dismissViewControllerAnimated:YES completion:nil];
1
9
2
0
The above code is very straightforward. We first hide the on-screen keyboard and
get the preset date from the date picker. Next, we create the local notification with
the date we just retrieve from the date picker. We also set the alert body by using
the to-do item text. Lastly, we increase the existing icon badge number by 1. With
everything configured, we fire up scheduleLocalNotification: method to schedule
the local notification.
Before we end the method, we notify the ToDoListViewController to refresh the
table data.
Displaying a List of Local Notification
In the main screen of the app, we display a list of local notifications that are
scheduled in the table view. Its pretty easy to retrieve the current scheduled local
notification. Simply make the following call and youll get an array of local
notifications:
1 [[UIApplication sharedApplication] scheduledLocalNotifications];
Select the ToDoListViewController.m and change the code for
1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2{
3
// Return the number of sections.
return 1;
}
4 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
5 (NSInteger)section
6{
// Return the number of rows in the section.
7
return [[[UIApplication
8
9 sharedApplication] scheduledLocalNotifications] count];
10 }
11
12 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
13 (NSIndexPath *)indexPath
14 {
static NSString *CellIdentifier = @"Cell";
15
UITableViewCell *cell = [tableView
16
17 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
18
19
// Get list of local notifications
20
NSArray *localNotifications = [[UIApplication
21 sharedApplication] scheduledLocalNotifications];
22
UILocalNotification *localNotification = [localNotifications
23 objectAtIndex:indexPath.row];
24
25
// Display notification info
26
[cell.textLabel setText:localNotification.alertBody];
27
[cell.detailTextLabel setText:[localNotification.fireDate description]];
return cell;
}
Ill not go into the details of the code. You should be very familiar with them if you
have an understanding to UITableView implementation.
Okay, lets compile and run the app. Tap the + button in the navigation bar and
add a to-do item. Set the date to a future date. Once done, tap save button to
schedule the notification. As soon as you go back to the table view, youll find the
notification just added. Go back to home screen. Wait for a couple of minutes
(depending on your preset schedule), you should see a notification banner. Or if
youre in lock screen, youll see a notification alert.
Playing a sound
By tapping on action button of the notification, users will launch the app. In this
case, the application:didFinishLaunchingWithOptions: method of the application
delegate is called.
Change the method to the following code:
- (BOOL)application:(UIApplication *)application
1 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2{
// Override point for customization after application launch.
3
4
5
// Handle launching from a notification
6
UILocalNotification *locationNotification = [launchOptions
7 objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
8
if (locationNotification) {
9
// Set icon badge number to zero
10
application.applicationIconBadgeNumber = 0;
11
}
12
13
return YES;
}
In the above code, we use the launchOptions dictionary and see if it contains a
local notification object. For the sake of simplicity, we just reset the icon badge
number when there is a local notification.
Applicaton is Running in Foreground
If the app is running while the notification is delivered, there is no alert displayed
on screen. The application automatically calls its
delegates application:didReceiveLocalNotification: method.
In the AppDelegate.m, add the following method:
1 - (void)application:(UIApplication *)application didReceiveLocalNotification:
2 (UILocalNotification *)notification
3{
4
UIApplicationState state = [application applicationState];
5
if (state == UIApplicationStateActive) {
6
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
7
message:notification.alertBody
8
delegate:self cancelButtonTitle:@"OK"
9
otherButtonTitles:nil];
1
[alert show];
0
1
1
}
1
2
// Request to reload table view data
1
3
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" obj
1
ect:self];
4
1
// Set icon badge number to zero
5
application.applicationIconBadgeNumber = 0;
1
}
6
1
7
As well discuss in the following section, the didReceiveLocalNotification: method
will also be called when application is running in background. Here we first
determine the application state and only display an alert when application is
running foreground. We also inform the view controller to reload the table date
and reset the applications icon badge number.