Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Push Notification in iOS

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 245
    Comment on it

    iOS Push Notification Tutorial:-

    Note:- Start following steps and dont do anything else. There are so many Gotchas while creating certificates but dont get confuse and follow these steps.

    Step 1:- Generating the Certificate Signing Request (CSR)

    Open Keychain Access on your Mac (it is in Applications/Utilities) and choose the menu option Request a Certificate from a Certificate Authority.

    alt text

    Image reference

    Please ensure that Let me specify key pair information is available. Check Saved to disk and click Continue. Save the file as PushChat.certSigningRequest.

    alt text

    Image reference

    Step 2:- If you go to the Keys section of Keychain Access, you will see that a new private key has appeared in your keychain. Right click it and choose Export. alt text

    Image reference

    Save the private key as PushChatKey.p12 and enter a passphrase.

    Step 3:

    Login to apple developer account

    Go to Identifiers section select AppId.

    Scroll down to the Push Notifications section and select the Create Certificate button in the Development SSL Certificate section.

    alt text

    Image reference:-

    Choose the CSR file that you generated earlier and click Generate. Click Continue when its done. Now click Download to get the certificate it is named aps_development.cer.

    alt text

    Image reference

    Now we have three things in hand:-

    The CSR
    The private key as a p12 file (PushChatKey.p12)
    The SSL certificate, aps_development.cer

    Note: Please dont think about any other .p12 or any other certificate other than these three which you have created just now. Keep all files altogether in a same folder or on Desktop.

    Step 4:-
    Now open Terminal window and type the following commands.
    Command 1 - cd ~/Desktop/

    Convert the .cer file into a .pem file:
    Command 2 - openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem

    Convert the private keys .p12 file into a .pem file:

    Command 3 - openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12
    Enter Import Password:
    MAC verified OK
    Enter PEM pass phrase:
    Verifying - Enter PEM pass phrase:

    Combine the certificate and key into a single .pem file:
    Command 4 - cat PushChatCert.pem PushChatKey.pem > ck.pem

    Test whether the certificate works.
    Command 5 - telnet gateway.sandbox.push.apple.com 2195
    Trying 17.172.232.226...
    Connected to gateway.sandbox.push-apple.com.akadns.net.
    Escape character is '^]'.

    Now press ctrl+C.

    This time using our SSL certificate and private key to set up a secure connection:
    Command 6 - openssl s_client -connect gateway.sandbox.push.apple.com:2195
    -cert PushChatCert.pem -key PushChatKey.pem
    Enter pass phrase for PushChatKey.pem:

    Open AppDelegate file of your project in XCode

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

    Get device token using:-

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

    For testing purpose download samplePush.php file and edit the following info in it...

    // Put your device token here (without spaces): $deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78';

    // Put your private key's passphrase here: $passphrase = 'pushchat';

    // Put your alert message here: $message = 'My first sample push notification!';

    *Final Step* Open terminal and type Command :- php samplePush.php

    Result :- you'll get "message successfully delivered".

    Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: