Sending apple push notification through PHP server

One of the widely used feature in iPhone development platform is to send the push notifications to the individual device relevant to the application installed on to it.

This tutorial will go into details of how to configure the apple push notification for the device application as well as how to send the push notification onto apple device using PHP server.

Step 1:Configure your iPhone application to receive push notification.

Find your application under the App ID’s on apple developer site.

Under your application click on configure to enable the APNS(apple push notification server)service.You have to create the push notification for both development as well as production mode of the app.

Follow the steps to configure the app for push notification by giving the certificate signing request.

To generate the certificate signing request go to keychain access and click on request a certificate from certificate authority.

Once you submit the CSR(certificate signing request)you will be able to configure the app for push notification as shown in the following image.

Once your are done with configuring the APNS service create provisioning profile for your application by selecting the configured app ID .If you create provisioning before configuring the app ID your application may not be able to receive the device token later explained in this article.

Step 2:Export the certificates to be used to send the push notification

Download the development certificate which then will be visible in your keychain access as shown in the below image.

Right click on that certificate and export as “certificate_development_push”. Do the same for the private key and save as “certificate_development_push_key” .

These two files are saved in .p12 format which we need to convert into .PEM format by executing command on terminal.

openssl pkcs12 -in

CertificateName

.p12 -out

CertificateName

.pem -nodes

Step 3:Combine the key and certificate files

Combine the development and key certificate files into one by using the following command.

cat dev-cert.pem dev-key-noenc.pem > apns-dev.pem

All the steps mentioned above need to be done for production certificate as well.

Step 4:PHP sample to send the push notification on apple device

The payload to be sent onto device via push notification is encoded into JSON.It contains:

  • Alert — the text string to display on the device
  • Badge — the integer number to display as a badge by the application icon on the device home screen

We can also add various option to display button on the alert text.Details can be found on apple developers site.

apple developer site link to add more custom feature to payload

via: developer.apple.com

Create a payload using PHP as follows:

$payload['aps'] = array('alert' => 'Test alert', 'badge' => 1, 'sound' => 'default'); $payload = json_encode($payload);

Note:

HTML element code may not appear properly in the editor. See preview.

There is a 256 bytes size limit on the data that can be sent to device using the payload.

Create a connection using PHP as follows:

$apnsHost = ‘gateway.sandbox.push.apple.com’;
$apnsPort = 2195;
$apnsCert = ‘apns-dev.pem’;

$streamContext = stream_context_create();
stream_context_set_option($streamContext, ‘ssl’, ‘local_cert’, $apnsCert);

$apns = stream_socket_client(‘ssl://’ . $apnsHost . ‘:’ . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

Now send the payload created above using the socket connection.

$apnsMessage = chr(0) . chr(0) . chr(32) . pack(‘H*’, str_replace(‘ ‘, ‘’, $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

Finally close the connection once all the above steps are done.

socket_close($apns);
fclose($apns);

This is how we send the push notification onto apple device using PHP.

APNS(apple push notification server) recognizes the device by the unique device token generated on the client side iOS application.

The device tokens generated onto client iOs application for development and production are different.It is not same as UDID.

Step 5:Register iOS client app to receive remote notification.

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.

self.viewController = [[ViewController alloc] initWithNibName:@”ViewController” bundle:nil];

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];

[[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];

returnYES;

}

– (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

{

NSString * deviceTokenStr=(NSString *)deviceToken;

NSLog(@”My token is: %@”, deviceTokenStr);

}

We need to store this device token onto our server to send the push notification onto device.

If the application fails to register for remote notification following callback is called.

– (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error

{

NSLog(@”Failed to get token, error: %@”, error);

}

In addition to this we can also use apple feedback services to check for the devices marked as inactive by APNS.

APNS marks the devices as inactive if the application is uninstalled from that device and we try to send the push notification.

It is a safe practice to check the inactive devices using the apple feedback service on regular basis.

Originally Posted: Cuelogic Blog

Leave a comment

Create a website or blog at WordPress.com

Up ↑