Skip to main content

IOS Push Notification PHP class using Apple Push Notification Service (APNs)

Apple Push Notification Service (APNs) is the center peace of the remote notification features. It is quick and highly efficient service for receiving notification throw-out the IOS devices. Apple Push Notification service sends and routes remote notifications for your apps from your provider to each user’s device. You can send notifications through APNs to many devices on which the client apps are installed. You can find more about APNs on official Apple developer site at https://developer.apple.com/. This tutorial is dedicated to Ankit Kansara who submitted a request throw our Request a Tutorial page for Ios PUSH NOTIFICATION PHP Class

Lets understand flow of IOS push notification.

Push Notification flow with PHP Web server and APNs

ios-notification-flow

 

  1. The App registers for push notification to IOS.
  2. IOS asks Apple Push notification service (APNs) for a device token.
  3. APNs generate unique device token and send it to the App.
  4. The App sends device token to PHP web server.
  5. PHP web server store device token to the database for later use.

 

  1. When something interesting happen PHP web server fetch device token from the database.
  2. PHP web server sends device token along with the push message to the APNs.
  3. APNs find the device using unique device token and send that push message to that particular device.

Configuring an App for Push Notifications

Before heading to the Push notification you first need to configure your app for the Push notification after that you have to register your app for the push notification that step will provide you Device token which is the unique key for each device. From your member center you also need to create SSL Certificate and PEM file which we will use as an authorization key during each push to the app. To understand the device token and SSL certificate flow you should read following steps from Ray Wenderlich Push Notification Tutorial

  1. Configuring an App for Push Notifications
  2. Registering for Push Notifications
  3. Creating an SSL Certificate and PEM file

 

IOS Push notification PHP Class

Create a php file called it iosNotification.php and add following code:

 

 

In development mode you should use $sandbox = true in this class, by default it’s false. When you turn on sandbox mode it will use apple sandbox push gateway ssl url which is ssl://gateway.sandbox.push.apple.com:2195 .

Add following Public function to the class.

 

This function is responsible for sending push notification to IOS device. Find the $filename = 'cert/pushcert.pem'; and $passphrase = 'password'; lines where pushcart.pem is the apple certificate .pem file with full path which we get from the 3rd step of Ray Wenderlich tutorial’s Creating an SSL Certificate and PEM file that I mentioned above. And $passphrase is the password we added while creating the .pem file.

 

Payload

Notice the $payload_body array which have badge, alert, sound and data key. badge is the unread notification count which we need to pass with the Notification. alert contains the Notification message. sound will contain default sound of the notification you can change the sound by adding .mp3 sound file here. data contains random data that you might need with your notification.

Rest of the code is responsible to connect to the APNs server, pass payload data in json format and return the response of push notification.

 

Wrapping up IosNotification Class file

Push Notification Call

Include the above IOS notification Class

include_once 'iosNotification.php';

call the notification class and add the your device token id(registration id) where I wrote YOUR_DEVICE_TOKEN_ID

wrapping notification class call.

Conclusion

You can download complete IOS Push notification class from above link. What we have is a very basic method for constructing, and sending push notifications to Apple’s Push Notification Service via PHP class.

Hardik Joshi

Hardik Joshi is Founder, Developer and Writer at eCodezone.com, He loves to write usable code for the Web and other digital sources.