Tuesday, March 20, 2012

Notifications in iPhone application

-  Rashid khaleefa
       There should be always a way to provide alerts a informations for any IOS applications. Apple brings in this feature to IOS through APNs which is Apple Push Notification Service. Lets have a quick follow through over the process of activating this service in iPhone app.
Before to go through the steps, listed below are the types of notifications that iOS apps provide
  • Sounds: Plays an audible alert
  • Alerts/Banners: Displays an alert or banner on the screen
  • Badges: Displays an image or number on the application icon
The Notification view has made pretty cool from iOS5 onwards that one can just swipe down for notifications from the top of the screen.

What APNS does ?

Apple Push Notification service transports and routes a notification from a given provider to a given device. APNS actually uses the Push technology by which the notification from the servers of third party applications are forwarded to Apple devices.

How to Enable APNS in our app?
To sum up the steps in order
  • Request for certificate Authority
  • Create app id
  • Configuring AppId for Push Notifications
  • Create provisioning profile
  • Provisioning a device
  • Enabling the profile in Xcode
  • Creating push notification provider.
Going into each steps further

Request for certificate Authority
  1. Open keyChain Access.
  2. Select Certificate Assistant > request a certificate from certified authority.
  3. Fill up the Email , common name fields.
  4. Click on save to disc which will save the certificate to your computer.
Creating AppId
  1. Login to apple member sender and navigate to iOS Provisioning Portal page.
  2. Click on the Apple Ids tab and select New App ID.
  3. Enter Description, Bundle Seed Id (select Generate new) and Bundle Identifier (reverse domain style is recommended)
  4. Click on the submit button.
You can know see the AppId created 

Configuring AppId for Push Notifications
  1. Click on the configure option to the right of the AppId listed.
  1. Enable the checkbox for push notifications.
  2. Click on the configure button which is to the right of Development Push SSL Certificate.
  3. Click continue.
  4. Upload the CSR file that is been saved before by clicking choose File button from Submit Certificate Signing Request screen and click Generate.
  5. Click continue opens next screen where you can download the APNs SSL Certificate.
  6. Double click the downloaded file to install it in the Keychain Access application.
Create Provisioning Profile
  1. Click on the provisioning tab.
  2. Enter profile name and select certificate, AppId , devices that you need to provision.
  3. Click on Submit.
  4. Now you can see a download button to the right of the provisioning profile created. Click on it to download the profile.
  5. Drag this file to the Xcode on the Dock.
  6. To provision a device connect the device and drag the profile to the xcode in doc. Open the organizer from window menu from xcode and you can see the profile installed on the device.
Enabling the profile in xcode
  1. Create a project in xcode
  2. Right click the file found below Targets. Choose GetInfo opens the target info window.
  3. Click on the build tab and select Code Signing Identity.
  4. Select the provision profile that was created before.
This is all you have to do. And its now time to code for bringing out the notification to our application. Its easy that you can add some delegates to the AppDelegate.m file 

- (void)applicationDidFinishLaunching:(UIApplication*)application
{
 // Add registration for remote notifications
 [[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |              UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// Clear application badge when app launches
 application.applicationIconBadgeNumber = 0;
}
//Fetch and Format Device Token and Register Important Information to Remote Server

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
 // code to register the device token to the provider
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
 NSLog(@"Error in registration. Error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
 NSLog(@"remote notification: %@",[userInfo description]);
 NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
 NSString *alert = [apsInfo objectForKey:@"alert"];
 NSLog(@"Received Push Alert: %@", alert);
}

And now creating a push notification provider
      The push notification provider sends notifications to application through APNs. For this you must first communicate with the APNs using SSL certificate that was created earlier. And then you can sent push notification containing the payload to the APNs. The pay load have to be constructed as below.
     {
          "aps": {
               "badge": 2,
               "alert": "This is Labs Team from CSSCorp."},
               "device_tokens":["8DCDDC0CC1E110228DC9C1926B301BA3C3ED7403D6A39D571A1CD81906D7C944"]
      }
To avoid the burden of creating a provider you can even use some third party service to make your work easy. I have tried with UrbanAirShip service. Below are the steps that I followed.

Step 1:  The first is to create an account with UrbanAirShip.

Step 2:  Create an a new application by clicking on create app option on Select an application tab. Enter the application name, Add application icon. Now select on push notification support which will expand the options. Now add the Apple push certificate file which we have already downloaded incase of an iPhone application.

Step 3:  This will display the full details of the app created where you can get the app key, app secret, device tokens etc:

Step 4:  Click on push tab. This opens a window where you can specify the device token, badge, alert, sound for the notification to be sent. Note that once entering all these fields the payload will be automatically created.

Step 5:  Click on the sent this will send the notification to the device.

To make this work on our application you have to add the library to the xcode project created and do a bit modification to your the Appdelegate.m file.

Further you can download a sample app using urbanAirShip notification here to know how to integrate the library into your xcode project.





7 comments:

sandeepbvnr said...

nice info

data recovery atlanta said...

APNS is distinction point for iPhone.. It is amazing.

K.Muthu said...

Nice One Dude.. Its really help me ..keep the good work ..:)

Safdar Ali said...

Nice post with great details. I appreciate your info. Thanks for sharing. mass notification software

Oliver Maurice said...

See info this blog post for some tips on how to write great essay and what to do if you get writer's block. Good luck

Anonymous said...

FAQ - 우리카지노 - 【바카라사이트】카지노사이트인포,메리트카지노,샌즈카지노

robertcooper said...

Your blog is great. I often visit such places where you can chat with interesting people. I often visit different sites https://primedissertations.com/write-my-marketing-plan/ for university studies. I can order extra help and get a good grade. I am happy to share some interesting information with you.