My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Tuesday 23 July 2013

Push notification in IOS

One thing you should know that IOS apps use different provisioning profiles for development and distribution.So push server certificates are different for development and distribution:
    •    Development. If your app is running in Debug mode and is signed with the Development provisioning profile (Code Signing Identity is “iPhone Developer”), then your server must be using the Development certificate.
    •    Production. Apps that are distributed as Ad Hoc or on the App Store (when Code Signing Identify is “iPhone Distribution”) must talk to a server that uses the Production certificate. If there is a mismatch between these, push notifications cannot be delivered to your app.


Steps for Push notification for development.

----------------------------------------------------
Generating the Certificate Signing Request (CSR)
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
3.Enter the information required and check the Saved to disk option. Click Continue
4.Save the certificate by clicking Save.
5.It will save as "CertificateSigningRequest" with ".certSigningRequest" extension.

Creating an App ID
Each iPhone applications that uses the push notification must have a unique application ID

Step 1: Login to iOS Provisioning Portal, click "Identifiers" on the right side. Make sure "App IDs" is selected, then click "+" button.

Step 2: Enter a short description of your app, check "Push Notification" service option, fill the "Bundle ID" field for App ID Suffix. For Bundle ID, we suggest you to use a reverse-domain name style string, e.g if your domain is "rajuandroid.com", enter "com.rajuandroid.yourAppName". Then, click "Continue" button to go to next step.

Step 3: Confirm your App ID information, then click "Submit" button to complete the registration.

Creating SSL Certificate

 
Step 1: Login to iOS Provisioning Portal, click "Certificates" on the left navigation bar. Then, click "+" button.

Step 2: Select Apple Push Notification service SSL (Sandbox) option under Development section, then click "Continue" button.

Step 3: Select the App ID you want to use for your BYO app , then click "Continue" to go to next step.

Step 4: Upload the ".certSigningRequest" file which is generated before, then click "Generate" button.

Step 5: Click "Done" to finish the registration, the iOS Provisioning Portal Page will be refreshed.

Step 6: Then Click "Download" button to download the certificate (.cer file) you've created just now

Step 7: Double click the downloaded file to install the certificate into Keychain Access on your Mac.


Step 8: On your Mac, go to "Keychain", look for the certificate you have just installed. If unsure which certificate is the correct one, it should start with "Apple Development IOS Push Services:" followed by your app's bundle ID.

Step 9: Expand the certificate, you should see the private key with either your name or your company name. Select both items by using the "Cmd" key on your keyboard, right click (or cmd-click if you use a single button mouse), choose "Export 2 items".

Step10:Then save the p12 file with name "DevelopmentPushCer.p12" to your Desktop.Now you will be prompted to enter a password to protect it, you can either click Enter to skip the password or enter a password you desire.

Creating PEM file

Step 11: Open "Terminal" on your Mac, and run the following commands:
cd
cd Desktop
openssl pkcs12 -in DevelopmentPushCer.p12 -out DevelopmentPushCer.pem -nodes -clcerts

Enter Import Password:
(Enter same password whatever given just above steps)
Step 12:Now "DevelopmentPushCer.pem" file will be created in your desktop.This .pem file is required for the server side implementation.


IOS code
==========
Now write some code in IOS app so that we can obtain a device token.When your app registers for remote (push) notifications, it tries to obtain a “device token”.
This is a 32-byte number that uniquely identifies your device.
e.g of device token
<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>

Step1:Make sure that you are using a development profile for Code Signing build settings.

Step2:Open AppDelegate.m. Change the application:didFinishLaunchingWithOptions: method to look like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    return YES;
}


Step3:In order to be able to receive push notifications. Add the following to AppDelegate.m:


 - (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    //NSLog(@"APNS device token is: %@", deviceToken);
  
    NSString *tokenStr = [deviceToken description];
    NSString *pushToken = [[tokenStr
                              stringByReplacingOccurrencesOfString:@"<" withString:@""]
                             stringByReplacingOccurrencesOfString:@">" withString:@""]
                            ;

   // NSLog(@"pushToken is: %@", pushToken);
//write your code to send this pushToken to your server.so that your server can send notification to this device.
       
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}




ServerSide code
============
Implement some code for sending push notification.

1 comment:

  1. think your blog is pretty good which have some useful information. Thanks for sharing us!
    Your contents are too simple to read and easy to understand.
    --------------------------------------------------------------------------------------------------------------
    iPhone App Development :: && ::Mobile and Web Application Development

    ReplyDelete