Last updated on July 31st, 2016 at 11:13 am

Apple push notification service (APNS) is widely used to send notification to apple powered device like iPad, iPhone etc.

APNS apple

Apple Push Notification Services (APNS) includes :-

  1. Custom Text Alerts
  2. Badges
  3. Sounds

Implementation of APNS to your application :-

For implementing push notifications in your application , you first need a paid developer account and a iOS Device (Either iPhone or iPad) because push notifications can’t be tested on iOS Simulator.

Step 1 : Create CSR Certificate

  1. Go to Keychain access in your Mac.
  2. Click On Certificate Assistant  >>>> Request a Certificate from Certificate Authority.
  3. Enter your details and save it to the disk.

Step 2 : Create p.12 file 

  1. Find the certificate which you`ve  created in My Certificates.
  2. Click on the certificate and expand it .  You will see Private Key.
  3. Select both of them and click on Export.
  4. Enter your phase password and then your mac password .
  5. Your p12 file have been created.

Step 3 : Add your app Id in developer`s account

  1. Open developer.apple.com and login in your account
  2. Create a new app id and click on configure Push notifications.
  3. There are two push notification certificates  (Development and Distribution). As we want to test the certificates for development , click on development push notification certificate.
  4. Upload the CSR which you`v created earlier on the upload CSR tab and download your push notification certificate.

Step 4 : Add provisioning profile :

  1. Create a new development certificate and add your devices in it .
  2. Download the provisioning profile.

Step 5 : Convert your .p12 file into .pem

For APNS , you need to convert your P12 file (which contains certificate and private key) into .PEM file

  1. For that , Go the terminal in your mac
  2. Type cd space location where you`v save p12 file . e.g. cd Saurabh
  3. Type openssl pkcs12 -in apns-dev-cert.p12 -out apns-dev-cert.pem -nodes -clcerts (where  apns-dev-cert.p12 is my p12 file which will get converted into apns-dev-cert.pem )
  4. It will ask for your phase password which you`v set earlier. Enter that.
  5. Your .pem file will be created

Upload this file in the php end .

Step 6 : Open Xcode and add apns methods in your AppDelegate.m


– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
// Let the device know we want to receive push notifications
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// for iOS 8
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
application.applicationIconBadgeNumber = 0;
// Register for Push notifictions . It will ask to recieve push notifactions when the app starts .
return YES;
}
– (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
// Send this device token to recieve Push notifications
– (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
// Shows error if Push notifactions can`t be sent . This method will display error .

view raw

gistfile1.txt

hosted with ❤ by GitHub

That’s all about APNS in iOS applications .

Now at server end i use php to send notification on device you just copy bellow code and replace file name with your file name and add password you set.


<?php
// Put your device token here (without spaces):
$deviceToken = '<DEVICE_TOKEN>';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = 'apns-dev-cert.pem';
$apnsPort = 2195;
$apnsPass = '<PASSWORD>';
$token = $deviceToken;
$payload['aps'] = array('alert' => 'Oh bilo!', 'badge' => 1, 'sound' => 'default');
$output = json_encode($payload);
$token = pack('H*', str_replace(' ', '', $token));
$apnsMessage = chr(0).chr(0).chr(32).$token.chr(0).chr(strlen($output)).$output;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $apnsPass);
$apns = stream_socket_client('ssl://'.$apnsHost.':'.$apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
fwrite($apns, $apnsMessage);
fclose($apns);

Thank you 🙂