Apple Push Notification Service (APNS)

iOS Developer Portal

The main purpose here is to set up the Apple Push Notification service SSL Certificates for both sandbox (development) and production environments.

In summary, the steps are:
1) Generate a Certificate Request. Normally, this CSR (Cert Signing Request) should have already been generated as the first step to configure a development certificate. Reuse the CSR if it is available, if not run “Keychain” -> Access Certificate Assistant -> Request a Certificate From a Certificate Authority and save the CSR to a file

2) Create an App ID with a specific Bundle Seed ID. Note that bundle IDs with wildcards will not be eligible to configure for push notifications. Goto your provisioning profile and use this App ID.

3) Once this App ID has been created, goto the Configure link to enable the Apple Push Notification service for this App ID. Reuse the CSR file in step 1 to generate the Development and Production Push certificates.

4) Download both certificates and save as .cer files

5) Double click both .cer files to install in Keychain Access and export the APN Certificates. For the development cert, select both the cert and the private key entries in Keychain Access and export to a .p12 file and enter a keystore password for the file. Do the same for the production cert and there will be two .p12 files which will be used as keystores for the Provider

Reference: http://mobiforge.com/developing/story/programming-apple-push-notification-services
Reference: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html

Apple Push Notification Service Provider for Java (Java PNS)

This is the provider/server which will be sending the push notifications to the iOS devices. It does this by sending messages to the APNS which will then send the message to the target devices.

Java PNS is suitable for backend systems running Java. There are some commandline tools to get started here: http://code.google.com/p/javapns/wiki/CommandLineTools

The development .p12 file will be used to do the SSL authentication to the APNS. But the other important parameter required is the “device token”.

Reference: http://code.google.com/p/javapns/wiki/ReadMeFirst

iOS App and Device Token

The iOS app has to be set up to register push notifications and get back a device token. This token will be used by the APNS as the destination ID to set messages to. The Java PNS provider will use this device token to send a message.

In XCode, the Code Signing certificate would be the development certificate that has been configured to use the specific App ID configured with push notifications.

The following codes in AppDelegate.m will register the device for push notifications and receive a unique device token such as “a11ee50de6531b8a6f36a7aa30035df7c9e26b555a883d741c5e3d9bcd7961fb”:
- (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:@"ios_device_token=%@",deviceToken];self.invokeString = str; self.invokeString = str; NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", 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]);
}

}

This device token will need to be passed to the HTML5 app and this is done by setting the device token to the invokeString variable which will be available in the HTML5 as a JS variable. This is because for each user account, we need to store the device token, so that messages related to this account can be sent to the correct device. Therefore, the HTML5 app will need to know the device token and then call a webservice on the server to store the device token with the logged in user ID.

Reference: http://mobiforge.com/developing/story/programming-apple-push-notification-services

Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: