I have found that the different types of notifications in iOS and how these notifications relate to Notification Center, are a source of great confusion. Recently, I gave a talk at MobiDev Philly on the subject. This, and my next blog post, summarize the talk. This post gives an overview and goes into a bit of detail on NSNotificationCenter and Notification Center. Part Two will address UILocalNotification and Remote Notifications.
Apple provides three different types of notifications in iOS: NSNotificationCenter, UILocalNotification, and Remote Notifications. In addition, there has been a Notification Center to manage notifications since iOS 5. The three notifications have three very different uses. NSNotificationCenter and Notification Center share a similar name, but are not related. For these reasons, there seems to be much confusion around notifications.
The following table summarizes the three types of notifications highlighting the differences between them.
Notification Center is a feature added in iOS 5 that allows users to control and manage local and remote notifications. One part of Notification Center is the window shade pulled down from the status bar showing active notifications. The other part of Notification Center is accessed from Settings → Notification Center. It allows users to set the type of alert displayed for notifications, change the sound played or even disable notifications completely.
Note that Notification Center manages notifications generated by UILocalNotification and Remote Notifications only. Notifications from NSNotificationCenter (within an app) are not managed by Notification Center, making the naming confusing. NSNotificationCenter dates back to Mac OSX 10.0 while Notification Center was added in iOS 5, so there are historical reasons for this unfortunate naming.
Since the very first release of MacOS X, NSNotificationCenter has been used to send notifications within an app. These notifications are invisible to users, but allow one part of an app to let another part know something has happened and some action is required. A typical use for this is to notify a view that a download of data from a remote server is complete, and the data needs to be processed and the view updated to reflect it.
A significant advantage of NSNotificationCenter notifications is that the action can be changed between triggers, so the same notification can have different actions at different times. Also, multiple methods can register to receive any notification.
The programmer must remember to remove the observer for notifications when they’re no longer needed. This is often done in the dealloc method for the class where the notification was registered. The dealloc method exists and is called even when ARC is used for memory management.
NSNotificationCenter vs. Delegates
NSNotificationCenter can be used in a similar way to delegates, notifying a different part of the app that some action is required. Actions called via NSNotificationCenter can be changed while a delegate method is always the same. It’s also possible to have one trigger cause several different actions. A class can be set up to support multiple delegates, but this is complex.
As a general rule, if a single, non-changing action is required for a specific trigger, a delegate method may be the best option. If a trigger must sometimes result in one action and other times have a different action, or if a trigger needs to result in multiple actions in different locations, NSNotificationCenter is a better choice. However, the choice is often not straightforward and the decision often a matter of style.
In Part Two, I’ll discuss UILocalNotification and Remote Notifications. Keep an eye out for it.
In the meantime, if you have any thoughts or suggestions, please share them in the comments below.