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.
Apple Push Notification Services (APNS) includes :-
- Custom Text Alerts
- Badges
- 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
- Go to Keychain access in your Mac.
- Click On Certificate Assistant >>>> Request a Certificate from Certificate Authority.
- Enter your details and save it to the disk.
Step 2 : Create p.12 file
- Find the certificate which you`ve created in My Certificates.
- Click on the certificate and expand it . You will see Private Key.
- Select both of them and click on Export.
- Enter your phase password and then your mac password .
- Your p12 file have been created.
Step 3 : Add your app Id in developer`s account
- Open developer.apple.com and login in your account
- Create a new app id and click on configure Push notifications.
- There are two push notification certificates (Development and Distribution). As we want to test the certificates for development , click on development push notification certificate.
- Upload the CSR which you`v created earlier on the upload CSR tab and download your push notification certificate.
Step 4 : Add provisioning profile :
- Create a new development certificate and add your devices in it .
- 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
- For that , Go the terminal in your mac
- Type cd space location where you`v save p12 file . e.g. cd Saurabh
- 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 )
- It will ask for your phase password which you`v set earlier. Enter that.
- 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 . | |
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 🙂