Implementing Local Notifications
Earlier in this lesson, you saw a short snippet of the code necessary to generate a local notification (UILocalNotification). As it turns out, there's not much more you'll need beyond those few lines! To demonstrate the use of local notifications, we'll be updating Hour 10's "Getting the User's Attention" doAlert method. Instead of just displaying an alert, it will also show a notification 5 minutes later and then schedule local notifications to occur every day thereafter.
Common Notification Properties
You want to configure several properties when creating notifications. A few of the more interesting of these include the following:
- applicationIconBadgeNumber: An integer that is displayed on the application icon when the notification is triggered
- fireDate: An NSDate object that provides a time in the future for the notification to be triggered
- timeZone: The time zone to use for scheduling the notification
- repeatInterval: How frequently, if ever, the notification should be repeated
- soundName: A string (NSString) containing the name of a sound resource to play when the notification is triggered
- alertBody: A string (NSString) containing the message to be displayed to the user
Creating and Scheduling a Notification
Open the GettingAttention application and edit the doAlert method so that it resembles Listing 21.2. (Bolded lines are additions to the existing method.) Once the code is in place, we'll walk through it together.
Listing 21.2.
1
: -(IBAction
)doAlert:(id
)sender {2
:UIAlertView
*alertDialog;3
:UILocalNotification
*scheduledAlert;4
:5
: alertDialog = [[UIAlertView
alloc
]6
:initWithTitle
:@"Alert Button Selected"
7
:message
:@"I need your attention NOW (and in alittle bit)!"
8
:delegate
:nil
9
:cancelButtonTitle
:@"Ok"
10
:otherButtonTitles
:nil
];11
:12
: [alertDialogshow
];13
: [alertDialogrelease
];14
:15
:16
: [[UIApplication
sharedApplication
]cancelAllLocalNotifications
];17
: scheduledAlert = [[[UILocalNotification
alloc
]init
]autorelease
];18
: scheduledAlert.applicationIconBadgeNumber
=1
;19
: scheduledAlert.fireDate
= [NSDate
dateWithTimeIntervalSinceNow
:300
];20
: scheduledAlert.timeZone
= [NSTimeZone
defaultTimeZone
];21
: scheduledAlert.repeatInterval
=NSDayCalendarUnit
;22
: scheduledAlert.soundName
=@"soundeffect.wav"
;23
: scheduledAlert.alertBody
=@"I'd like to get your attention again!"
;24
:25
: [[UIApplication
sharedApplication
]scheduleLocalNotification
:scheduledAlert];26
:27
: }
First, in line 3, we declare scheduledAlert as an object of type UILocalNotification. This local notification object is what we set up with our desired message, sound, and so on, and then pass off to the application to display sometime in the future.
In Line 16, we use [UIApplication sharedApplication] to grab our application object, and then call the UIApplication method cancelAllLocalNotifications. This cancels any previously scheduled notifications that this application may have made, giving us a clean slate.
Line 17 allocates and initializes the local notification object scheduledAlert. Because the notification is going to be handled by iOS rather than our GettingAttention application, we can use autorelease to release it.
In line 18, we configure the notification's applicationIconBadgeNumber property so that when the notification is triggered, the application's badge number is set to 1 to show that a notification has occurred.
Line 19 uses the fireDate property along with the NSDate class method dateWithTimeIntervalSinceNow to set the notification to be triggered 300 seconds in the future.
Line 20 sets the timeZone for the notification. This should almost always be set to the local time zone, as returned by [NSTimeZone defaultTimeZone].
Line 21 sets the repeatInterval property for the notification. This can be chosen from a variety of constants, such as NSDayCalendarUnit (daily), NSHourCalendarUnit (hourly), and NSMinuteCalendarUnit (every minute). The full list can be found in the NSCalendar class reference in the Xcode developer documentation.
In Line 22, we set a sound to be played along with the notification. The soundName property is configured with a string (NSString) with the name of a sound resource. Because we already have soundeffect.wav available in the project, we can use that without any further additions.
Line 23 finishes the notification configuration by setting the alertBody of the notification to the message we want the user to see.
When the notification object is fully configured, we schedule it using the UIApplication method scheduleLocalNotification (line 25). This finishes the implementation!
Choose Build and Run to compile and start the application on your iPhone or in the iPhone Simulator. After GettingAttention is up and running, click the Alert Me! button. After the initial alert is displayed, click the Home button to exit the application. Go get a drink, and come back in about 4 minutes and 59 seconds. At exactly 5 minutes later, you'll receive a local notification, as shown in Figure 21.4.
Figure 21.4 Local notifications are displayed onscreen even when the application isn't running.