Tag Archives: iOS

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]
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |


- (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];


- (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

Tagged , ,

Getting Started with PhoneGap (iOS)

The main steps are already detailed on the official PhoneGap site – http://phonegap.com/start.

Below are my tried and tested steps for your reference to get the iOS environment up and a test App running on a Mac OSX (Lion) machine. It is not intended to be comprehensive and there are more details available on the web. Once you got the environment up and deploy the test app to your device successfully, you can add in the steps to create a PhoneGap app from the official guide above.

Getting Started with Running an iOS App in XCode4

1) Ensure that your Mac machine is running on Lion OSX 10.7 Mac OS X 10.7 because XCode 4.2.1 with SDK 5 only runs on Lion.

2) Go to the App Store app on your Mac machine and search for XCode4 and install it.

3) Download and extract SimpleDrillDown.zip (a sample iOS app) by double-clicking it and it can be downloaded from: http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007416

4) Open the SimpleDrillDown folder and double-click on SimpleDrillDown.xcodeproj to load this project in XCode4.

5) From the dropdown box above, select the iPhone Simulator (any version 4.3 or 5.0). Click on Run to build and run the project.

6) iPhone Simulator will load and run the SimpleDrillDown app.

Configure to Deploy on An Actual Device (Done Once Only)

1) Login to iOS Dev Center (https://developer.apple.com/devcenter/ios/index.action) using your Apple ID. You must have a paid subscription (at USD$99 per year) to the iOS Developer Program in order to log into this portal.

2) Go to the iOS Provisioning Portal

The license is tied to ONE main Mac machine only. So if you are changing to a different Mac machine, be sure to revoke the Current Development Certificate in the Provisioning Portal first. Then follow the steps to generate the CSR to disk and submit and download and use the new Development Certificate.
However, a better way is to export the Development certificate with the private key and import into the 2nd Mac machine.

3) Scroll to the bottom to “Get your application on an iOS with the Development Provisioning Assistant” and click on “Launch Assistant” and click “Continue” and follow the steps.

4) Assuming all the necessary are set up, connect your iOS device to your Mac machine, e.g. an iPhone.

5) Open the SimpleDrillDown folder and double-click on SimpleDrillDown.xcodeproj to load this project in XCode4.

6) From the dropdown box above, select the first option which should be the name of your iOS device. Click on Run to build and run the project.

Adding Another Device

1) Connect the new device to the Mac machine.

2) Open XCode4 and go to Window -> Organizer.

3) Select the new device from the list and click on the “Add to Portal” button.

4) Enter your Apple ID and password when prompted.

5) If there are no errors, you should be able to run the iOS app on the new device.

Tagged , , ,