17.5. Listening for and Reacting to Local Notifications
Problem
You know how to schedule local notifications (see Recipe 17.4), but now you have to react to them when they are delivered to your app.
Solution
Implement the application:didReceiveLocalNotification:
method of your app delegate and read the UIApplicationLaunchOptionsLocalNotificationKey
key of your app’s launching options dictionary when the application:didFinishLaunchingWithOptions:
method gets called on your app delegate. Read the Discussion section of
this recipe for more information on why you have to handle a local
notification in two places instead of just one.
Discussion
Depending on the state of your app when a local notification is delivered and acted upon, you will have to handle it differently. Here are the different situations in which iOS may deliver a scheduled local notification to your app:
- The user has the app open in front of her while the local notification is delivered
In this case, the
application:didReceiveLocalNotification:
method is called when the notification is delivered.- The user has sent the app to the background and the local notification is delivered
Once the user taps on the notification, iOS can launch your app. In this case, again, the
application:didReceiveLocalNotification:
method of your app delegate is called.- The app is not open or at all active when the local notification is delivered
In this case, the
application:didFinishLaunchingWithOptions:
method of your app delegate is called and the ...
Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.