To remedy this limitation, Apple released the Apple Push Notification Service (APNs). The APNs is a service that allows your device to be constantly connected to Apple's push notification server. When you want to send a push notification to an application installed on the users' devices, you (the provider) can contact the APNs so that it can deliver a push message to the particular application installed on the intended device.
In this article, you will learn how to perform the various steps needed to create an iPhone application that uses the APNs.
Generating a Certificate Request
The first step to using the APNs is to generate a certificate request file so that you can use it to request for a development SSL certificate later on.1. Launch the Keychain Access application in your Mac OS X.
2. Select Keychain Access'Certificate Assistant'Request a Certificate From a Certificate Authority (see Figure 1):
Figure 1. Generating a certificate request
Figure 2. Saving the certificate request to disk
Figure 3. Naming the certificate request
Creating an App ID
Each iPhone applications that uses the APNs must have a unique application ID that uniquely identifies itself. In this step, you will learn how to create an App ID for push notification.1. Sign in to the iPhone Developer Program at: http://developer.apple.com/iphone/. Click on the iPhone Developer Program Portal on the right of the page (see Figure 4).
Figure 4. Launching the iPhone Developer Program Portal
Figure 5. The welcome screen of the iPhone Developer Program Portal
Figure 6. Clicking on the App ID tab
net.learn2develop.MyPushApp
. Click Submit (see Figure 7). Figure 7. Creating a new App ID
Figure 8. Viewing the newly created App ID
Configuring an App ID for Push Notifications
Once an App ID is created, you need to configure it for push notifications.1. To configure an App ID for push notification, you need to click the Configure link displayed to the right of the App ID. You will now see the option (see Figure 9).
Figure 9. Configuring an App ID for push notification service
2. You will now see the Apple Push Notification service SSL Certificate Assistant screen. Click Continue (see Figure 10).
Figure 10. The Apple Push Notification service SSL Certificate Assistant screen
Figure 11. Generating the SSL certificate
Figure 12. The APNs SSL certificate that is generated
Figure 13. Downloading the certificate generated
aps.developer.identity.cer
.
Double-click on it to install it in the Keychain Access application
(see Figure 14). The SSL certificate will be used by your provider
application so that it can contact the APNs to send push notifications
to your applications. Figure 14. Installing the generated certificate into the Keychain Access application
Creating a Provisioning Profile
The next step is to create a provisioning profile so that your application can be installed onto a real device.1. Back in the iPhone Development Program Portal, click on the Provisioning tab and click on the New Profile button (see Figure 15).
Figure 15. Selecting the Provisioning tab
MyDevicesProfile
as the profile name. Select PushAppID as the App ID. Finally, check all
the devices that you want to provision (you can register these devices
with the iPhone Developer Program Portal through the Devices tab). Click
Submit (see Figure 16).Figure 16. Creating a new provisioning profile
Figure 17. Pending the approval of the provisioning profile
Provisioning a Device
With the provision profile created, you will now install it onto a real device.1. Connect your iPhone or iPod Touch to your Mac.
2. Drag and drop the downloaded
MyDevicesProfile.mobileprovision
file onto the Xcode icon on the Dock. 3. Launch the Organizer application from within Xcode and select the device currently connected to your Mac. You should see the
MyDevicesProfile
installed on the device (see Figure 18).Figure 18. Viewing the installed provisioning profile
Creating the iPhone Application
1. In Xcode, create a new View-Based Application project and name it as ApplePushNotification.2. Drag and drop a WAV file (shown as
beep.wav
in this example) onto the Resources folder in Xcode (see Figure 19). Figure 19. Adding a WAV file to the project
Figure 20. Entering the App ID for the application
net.learn2develop.MyPushApp
.4. Click on the Build tab and type "Code Signing" in the search box. In the Any iPhone OS Device item, select the profile as shown in Figure 21:
Figure 21. Selecting the profile for code signing
ApplePushNotificationAppDelegate.m
file, type the following code in bold:#import "ApplePushNotificationAppDelegate.h" #import "ApplePushNotificationViewController.h" @implementation ApplePushNotificationAppDelegate @synthesize window; @synthesize viewController; - (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview:viewController.view]; [window makeKeyAndVisible]; NSLog(@"Registering for push notifications..."); [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; } - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken]; NSLog(str); } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@", err]; NSLog(str); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { for (id key in userInfo) { NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); } } - (void)dealloc { [viewController release]; [window release]; [super dealloc]; } @end
Command-R
to test the application on a real device. Press Shift-Command-R
in Xcode to display the Debugger Console window. Observe carefully the
device token that is printed (see Figure 22). In the figure below, the
token is: 38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7c e90d56e9 fe145bcc 6c2c594b
. Record down this device token (you might want to cut and paste it into a text file). Figure 22. Viewing the device token for push notification
Figure 23. Viewing the Notifications item in the Settings application
Creating the Push Notification Provider
A Push Notification provider is an application written by the application's developer to send push notifications to the iPhone application through the APNs.Here are the basic steps to send push notifications to your applications via the Apple Push Notification Service (APNs):
1. Communicate with the APNs using the SSL certificate you have created earlier.
2. Construct the payload for the message you want to send.
3. Send the push notification containing the payload to the APNs.
The APNs is a stream TCP socket that your provider can communicate using a SSL secured communication channel. You send the push notification (containing the payload) as a binary stream. Once connected to the APNs, you should maintain the connection and send as many push notifications as you want within the duration of the connection.
Tip: Refrain from opening and closing the connections to the APNs for each push notification that you want to send. Rapid opening and closing of connections to the APNs will be deemed as a Denial-of-Service (DOS) attack and may prevent your provider from sending push notifications to your applications.
The format of a push notification message looks like Figure 24 (figure from Apple's documentation):
Figure 24. Format of a push notification message
The payload is a JSON formatted string (maximum 256 bytes) carrying the information you want to send to your application. An example of a payload looks like this:
{ "aps": { "alert" : "You got a new message!" , "badge" : 5, "sound" : "beep.wav"}, "acme1" : "bar", "acme2" : 42 }
1. Open the PushMeBaby application in Xcode.
2.
Right-click
on the Resources folder in Xcode and select Add Existing Files…. Select the aps.developer.identity.cer
file that you have downloaded earlier (see Figure 25). Figure 25. Adding the SSL certificate to the application
ApplicationDelegate.m
file, modify the code as shown in bold below:- (id)init { self = [super init]; if(self != nil) { self.deviceToken = @"38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7c e90d56e9 fe145bcc 6c2c594b"; self.payload = @"{\"aps\":{\"alert\":\"You got a new message!\",\"badge\":5,\"sound\":\"beep.wav\"},\"acme1\":\"bar\",\"acme2\":42}"; self.certificate = [[NSBundle mainBundle] pathForResource:@"aps_developer_identity" ofType:@"cer"]; } return self; }
Figure 26. Granting access to the SSL certificate
{ "aps": { "alert" : "You got a new message!" , "badge" : 5, "sound" : "beep.wav"}, "acme1" : "bar", "acme2" : 42 }
Figure 27. Receiving a Push Notification message
Command-R
and send a push message from the PushMeBaby application, the Debugger Console window will display the following outputs:2009-11-24 21:11:49.182 ApplePushNotification[1461:207] key: acme1, value: bar 2009-11-24 21:11:49.187 ApplePushNotification[1461:207] key: aps, value: { alert = "You got a new message!"; badge = 5; sound = "beep.wav"; } 2009-11-24 21:11:49.191 ApplePushNotification[1461:207] key: acme2, value: 42
没有评论:
发表评论