Recipe: Push Client Skeleton
Recipe 16-1 introduces a basic client that allows users to register and unregister for push notifications. The interface (shown in Figure 16-9) uses three switches that control the services to be registered. When the application launches, it queries the app's enabled remote notification types and updates the switches to match. Thereafter, the client keeps track of registrations and unregistrations, adjusting the switches to keep sync with the reality of the settings.
Figure 16-9 The Push Client skeleton introduced in Recipe 16-1 lets users specify which services they want to register.
Two buttons at the top left and right of the interface let users unregister and register their application. As mentioned earlier in this chapter, unregistering disables all services associated with the app. It provides a clean sweep. In contrast, registering apps requires flags to indicate which services are requested.
When requesting new services, the user is always prompted to approve. Figure 16-10 shows the dialog that appears. The user must confirm by explicitly granting the application permission. If the user does not, by tapping Don't Allow, the flags remain at their previous settings.
Figure 16-10 Users must explicitly grant permission for an application to receive remote notifications.
Unfortunately, the confirmation dialog does not generate a callback when it is dismissed, regardless of whether the user agreed or not. To catch this event, you can listen for a general notification (UIApplicationDidBecomeActiveNotification) that gets generated when the dialog returns control to the application. It's a hack and is not guaranteed to work in the long term, but at the time of writing, Apple has not provided any other way to know when the user responded and how the user responded. In Recipe 16-1, the confirmationWasHidden: method catches this notification and updates the switches to match any new registration settings.
Being something of a skeletal system, this push client doesn't actually respond to push notifications beyond showing the contents of the user info payload that gets delivered. Figure 16-9 illustrates the actual payload that was sent in Figure 16-10. This display is performed in the application: didReceiveRemoteNotification: method in the application delegate.