29
Push Notifications and Networking

On an iOS device, only one application can run in the foreground at a time. Sometimes, you want an application that isn’t currently running to notify you when something happens: an opponent in a game makes a move, a buddy sends you a message in a chat program, or you have 5 minutes to get to your next meeting.

Push notifications are one approach to solving this problem. A push notification is a message sent from Apple’s servers to a device. The user sees a pop-up window on screen with the name of the application that has been notified, a message, and possibly an alert sound or icon badge.

It’s not a coincidence that it is difficult to find a working implementation of push notifications on the Net – this stuff isn’t easy. You have to create an SSL certificate, provision the application, and write the network code to connect to Apple’s server.

But, in this chapter, we’re going to get it all done using the applications you created in Chapter 28. You will update CocoaServer to send push notifications and update Notified to receive them. Push notifications also require a middle man (Apple’s push notification server) and a device token, which uniquely identifies an iOS device. Figure 29.1 gives you an idea of the relationships between the three players.

Figure 29.1  Flow of a push notification

Flow of a push notification

Preparing Client for Push Notifications

To get Notified ready to receive push notifications, we must register for notifications with the Apple push notification server and then create a provisioning profile that allows for push notifications.

Registering for notifications

Registering for notifications does two things: it sets the types of notification the application will accept, and it retrieves the required device token from Apple’s server.

There are three types of notifications: alerts, badges, and alert sounds. (Badges are small numeric icons that appear on an application’s icon. You’ve probably seen them on the Mail and App Store icons when you have unread mail or application updates to download.) When an application registers for push notifications, it chooses the types of notifications it wishes to receive. When a notification is delivered, the operating system only delivers the types that the application has registered for.

Reopen NotifiedClientServer.xcworkspace. In NotifiedAppDelegate.m, register for all notification types at the top of application:​didFinishLaunchingWithOptions:.

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​/​/​ ​R​e​g​i​s​t​e​r​ ​(​e​v​e​r​y​ ​t​i​m​e​ ​t​h​e​ ​a​p​p​ ​l​a​u​n​c​h​e​s​)​ ​f​o​r​ ​n​o​t​i​f​i​c​a​t​i​o​n​s​
 ​ ​ ​ ​[​[​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​s​h​a​r​e​d​A​p​p​l​i​c​a​t​i​o​n​]​ ​r​e​g​i​s​t​e​r​F​o​r​R​e​m​o​t​e​N​o​t​i​f​i​c​a​t​i​o​n​T​y​p​e​s​:​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​R​e​m​o​t​e​N​o​t​i​f​i​c​a​t​i​o​n​T​y​p​e​A​l​e​r​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​|​ ​U​I​R​e​m​o​t​e​N​o​t​i​f​i​c​a​t​i​o​n​T​y​p​e​B​a​d​g​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​|​ ​U​I​R​e​m​o​t​e​N​o​t​i​f​i​c​a​t​i​o​n​T​y​p​e​S​o​u​n​d​]​;​

In addition to setting the valid notification types for this application, this code will make a network connection to Apple’s push notification servers to retrieve a device token. The device token is a 32-byte string of binary data that uniquely identifies the registering device. You can think of it like a phone number that Apple dials each time it wants to deliver a notification. Once Apple has generated the device token, it is returned to your device and delivered to the registering application via the application delegate method application:​didRegisterForRemoteNotifications​WithDeviceToken:. (The device token will be the same each time you register, but re-registering keeps the servers up-to-date.)

In NotifiedAppDelegate.h, add an instance variable to hold this token.

@​i​n​t​e​r​f​a​c​e​ ​N​o​t​i​f​i​e​d​A​p​p​D​e​l​e​g​a​t​e​ ​:​ ​N​S​O​b​j​e​c​t​
 ​ ​ ​ ​<​U​I​A​p​p​l​i​c​a​t​i​o​n​D​e​l​e​g​a​t​e​,​ ​N​S​N​e​t​S​e​r​v​i​c​e​B​r​o​w​s​e​r​D​e​l​e​g​a​t​e​,​ ​N​S​N​e​t​S​e​r​v​i​c​e​D​e​l​e​g​a​t​e​>​
{​
 ​ ​ ​ ​N​S​D​a​t​a​ ​*​p​u​s​h​T​o​k​e​n​;​

In NotifiedAppDelegate.m, implement the delegate method.

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​R​e​g​i​s​t​e​r​F​o​r​R​e​m​o​t​e​N​o​t​i​f​i​c​a​t​i​o​n​s​W​i​t​h​D​e​v​i​c​e​T​o​k​e​n​:​(​N​S​D​a​t​a​ ​*​)​d​e​v​i​c​e​T​o​k​e​n​
{​
 ​ ​ ​ ​/​/​ ​W​e​'​l​l​ ​k​e​e​p​ ​t​h​i​s​ ​t​o​k​e​n​ ​a​n​d​ ​t​h​e​n​ ​a​t​t​e​m​p​t​ ​t​o​ ​s​e​n​d​ ​i​t​ ​t​o​ ​t​h​e​ ​s​e​r​v​e​r​
 ​ ​ ​ ​p​u​s​h​T​o​k​e​n​ ​=​ ​[​d​e​v​i​c​e​T​o​k​e​n​ ​r​e​t​a​i​n​]​;​

 ​ ​ ​ ​/​/​ ​S​h​o​r​t​l​y​,​ ​w​e​'​l​l​ ​c​h​a​n​g​e​ ​t​h​e​ ​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​o​f​ ​t​h​i​s​ ​m​e​t​h​o​d​ ​t​o​ ​s​e​n​d​ ​t​h​e​ ​d​e​v​i​c​e​
 ​ ​ ​ ​/​/​ ​t​o​k​e​n​ ​a​l​o​n​g​ ​w​i​t​h​ ​t​h​e​ ​d​e​v​i​c​e​ ​n​a​m​e​ ​t​o​ ​t​h​e​ ​s​e​r​v​e​r​
 ​ ​ ​ ​[​s​e​l​f​ ​p​o​s​t​I​n​f​o​r​m​a​t​i​o​n​T​o​S​e​r​v​e​r​]​;​
}​

Notice that this method triggers the postInformationToServer method that you wrote in Chapter 28. Now this message is sent at two separate times: when the device token is retrieved and when the net service is resolved. You’ll see why this is important in a moment.

If the application fails to register with Apple, a different delegate method is called. Typically, this occurs when there is no network connection available. Implement this method in NotifiedAppDelegate.m.

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​F​a​i​l​T​o​R​e​g​i​s​t​e​r​F​o​r​R​e​m​o​t​e​N​o​t​i​f​i​c​a​t​i​o​n​s​W​i​t​h​E​r​r​o​r​:​(​N​S​E​r​r​o​r​ ​*​)​e​r​r​o​r​
{​
 ​ ​ ​ ​[​s​t​a​t​u​s​L​a​b​e​l​ ​s​e​t​T​e​x​t​:​@​"​F​a​i​l​e​d​ ​t​o​ ​r​e​g​i​s​t​e​r​ ​w​i​t​h​ ​A​p​p​l​e​"​]​;​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​e​r​r​o​r​)​;​
}​

Provisioning for push notifications

For the registration to succeed, the application has to be provisioned for push notifications. This requires generating a push notification certificate and embedding it in a provisioning profile that the application uses. You are going to create a new provisioning profile for Notified. Then, when Notified is built, it will use the developer certificate from this profile to sign the application code. When the application registers for push notifications, a value from this provisioning profile will be used to verify that it is able to receive notifications.

To generate the provisioning profile and the push notification certificate, you must be the Team Agent of your iOS developer account.

Log into the portal at developer.apple.com/ios and enter the iOS Provisioning Portal. While in the portal, you’re going to create a new App ID, create and download the SSL certificate, and create and configure the new provisioning profile.

In the left navigation table, select the App IDs item and click New App ID. Name this App ID Notified and select Generate New from the pop-up menu (Figure 29.2). The Bundle Identifier item will need to be something unique. For example, mine is com.bignerdranch.Notified. You can replace the middle of the bundle identifier with your company name, and that should ensure its uniqueness. Note that you cannot use the wildcard character (*) for this App ID. Click Submit.

Figure 29.2  Generating an application ID

Generating an application ID

Back in the list of App IDs, click the Configure button on the same row as the Notified App ID. On the page that appears, click the checkbox to enable push notifications. Then, click the Configure button for the Development Push SSL Certificate (Figure 29.3).

Figure 29.3  Enabling development notifications

Enabling development notifications

Now an overlay window with an assistant will appear and walk you through the steps of generating an SSL certificate. This certificate will be used to encrypt and decrypt push notification data. Follow the instructions in this guide, and you will eventually download a file named aps_developer_identity.cer. Keep this file handy; you will use it soon.

Now, select Provisioning from the left navigation table in the provisioning portal and click New Profile. Name this profile whatever you wish. Select your developer certificate (the one you have been using to sign your applications when building on the device) from the list of certificates. Choose the Notified App ID from the pop-up menu, and then select your development device from the list of devices. Click Submit.

This will take you back to the list of provisioning profiles, and your new profile will say Pending. Refresh your browser, and a Download button will appear. Download your new profile and drag it onto the Xcode window.

You will want to use this push notification-enabled profile to deploy the Notified application to your device. For that to happen, the application’s bundle identifier must match the profile’s App ID. In Xcode, select the Notified project from the project navigator and the Notified target from the editor area. Then, select the Info pane and locate the Bundle Identifier key (Figure 29.4). Change it to exactly match the App ID in the provisioning profile you just created. (This value is case-sensitive!)

Figure 29.4  Changing the bundle identifier

Changing the bundle identifier

One more step and our client application will be ready to receive push notifications. In the Build Settings pane, locate the Code Signing Identity setting. Click the value column for this setting and select the Notified provisioning profile from the list. (There are a number of sub-items in this setting. Changing the top-level item should also change all of the sub-items. However, if you have previously set a sub-item’s values to something other than its parent’s setting, you may need to change it individually.) My Code Signing Identity setting looks like the one in Figure 29.5.

Figure 29.5  Build settings

Build settings

Build and run the application on a device. (You can’t test push notifications on the simulator.) The application will ask you if it is okay to accept push notifications – say yes!

Exit the application and open the Settings application. Select the Notifications item and then select Notified, which now appears in this list. This application registered for all three notification types, so there is a switch for each type. Make sure they are all switched on.

Congratulations! Notified can now receive push notifications. Now let’s turn to the server side.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.239.226