Tag Archives: UILocalNotification

How to react to Local Notifications in your iOS App

Now that we know how to create Local Notifications, let’s see how we can react to them. Say your app wasn’t running, and you wanted to know if it was launched as a result of a notification.

Photo 06-01-2014 13 36 27

In our AppDelegate we are passed a launchOptions dictionary, which will have a UIApplicationLaunchOptionsLocalNotificationKey option that will contain another dictionary we can give our notification. This probably sounds more complicated than it is… basically if this key is nil, then we were not launched as a result of a notification. If it’s not nil, it will contain options from the notification that launched it.

When we create our notification we can pass in a userInfo dictionary like so (see previous step for details):

Note that when the app is already running, the didFinishLaunching method is not called. So if the app was open in the background this approach won’t work.

To find out if your already running app has been brought into the foreground, you can implement the following delegate method in your AppDelegate. This will be called if a notification was received, and will include the same userInfo dictionary as above:

Note that this method will be called every time your app receives a notification, not just when it’s woken from the background.

How to create Local Notifications in your iOS App

Local and Push Notifications are very similar, the only difference is their origin. While Push Notifications are sent to Apple who in turn send them to your device, a Local Notification is created like a mix between an alert view and a timer. To the end user they look the same.

Photo 06-01-2014 13 32 27

To create the we can use an instance of the UILocalNotification class, followed by scheduling the notification:

When we run our app it’s a good ice ago cancel all existing notifications that may be hanging around. Otherwise, you’d have 4x the amount of notifications when you run the app 4 times in a row. Next we create our notification and customise it. You can also set a badge number and define your own sound files.

The alertAction is the button or slider text when shown on the lock screen, and the alertBody the actual message. You can even specify an alertLaunchImage that will be shown when the user hits the action button (like a launch image).

All that remains is to schedule the notification. In our example it will come up once every minute, as specified in the repeatInterval. Choose any NSCalendar unit you like (daily, weekly, hourly, etc).

To bring up a notification right away you can do it like this:

Local notifications are fired even if the app is not running, or if it’s in the background. Users can choose to opt-out of receiving those, and they can choose how notifications from your app will appear (under Settings – Notifications – Your App).

Note that if your app is in the foreground notifications are not displayed (because that would be really annoying).