O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required