We are now Leftshift!
Click here to go to our new website

The Discussion Delegate

Answers to the Ultimate Questions about the Web, Mobile and Vercingetorix

Push Notifications in your iPhone App using PHP and Ubuntu

This post was created on February 3rd, 2010 by Sudhanshu and has 5 comments. It has been filed under , , , , ,

Push Notifications have been around for quite some time now, but when we started on it, we found out that we were unable to find all the information about creating a demo in one place. After a few hours, we were able to get it to work. Here is what we did.

Basically, the code on your server needs to talk to the Apple Push Notification Service (APNS) to send the messages, and the APNS then delivers them to your phone. We will only provide the code level details here to help you carry out the same on your server.

Basic Idea

You need to do the following to get your application to send push notifications

1. Make your application accept Push Notifications
2. Create a SSL certificate for your server which will allow it to talk to the APNS
3. Create the payload for each message to be send to the APNS
4. Send all the payloads one by one to the APNS

Even if you have already been into iPhone development for sometime, you might not be aware of the ‘DevToken’. This is a uniquely generated token for your iPhone application on a particular phone. It can keep changing over time for a particular phone as well, but it is the unique id that will help you send a message to a particular application on a particular phone.

This is what it looks like on our phone

4d3d32e4 c1beebd2 f3a6bc85 3f7d12b9 abc156b4 26818ecf 9673a712 85e01adc

This is what you need to do to get your device token.

Getting the certificates in place

Reach for your mac and start doing the following

1. Login to the iPhone Developer Connection Portal and click on App Ids.

2. Create an AppId for your application witouth a wildcard. It should be something like this :


3. Click configure and then go ahead and create a certificate for Push Notifications.[1]. Download it once it has been created.

4. Import the newly created certificate into your keychain by double clicking it.

5. Launch ‘Keychain Assistant’ and filter it by the Certificate’s category. There you should see a ‘Apple Development Push Services’ option. Expand it, right click on it and click on ‘Export …’ and save this as apns-dev-cert.p12 . Also download the private key as apns-dev-key.p12

6. Copy the file apns-dev-cert.p12 to your server in the folder where you will put the rest of your PHP code.

7. Now run the following code on the server
openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12

We are running everything from a server running Ubuntu-9.04. Here we had to remove the passphrase, which can be done as follows
openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem

Finally, combine the two to get your apns-dev.pem file
cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem

8. You need to create a payload and send it to the APNS server. You can use the following code to do so

9. Great work! You have just pushed a message to the APNS which will now be delivered to a phone running your application.

10. (Optional) Apple also provides a Feedback service (which doesn’t do anything that you would expect). This service tells you when was the last time that APNS failed to find your application on a specific phone. When this happens, you need to stop sending updates to that Device Token.

To assist you in your endeavors, here is the code to help you read this data

That’s it. You are now officially a pro at push notifications!

References :
1. Apple’s Documentation
2. BoxedIce

Responses are currently closed.
We are a Pune, India based firm specializing in building PHP, iPhone and Android applications. You can also catch our latest updates via RSS or follow us on Twitter. To invite us to work on a project, please get in touch via the Contact page and we'll get back to you at the earliest.

5 Responses to “ Push Notifications in your iPhone App using PHP and Ubuntu ”

  1. Duccio says:

    There is an open source PHP classes collection for APNS: ApnsPHP

    With this comprehensive collection of open source PHP classes you can also easily create a Push Server with one or more (forked) processes reading from a common message queue to speed-up sending activities.

    Google Code: http://code.google.com/p/apns-php/

  2. Matt says:

    This is excellent, I am in the process of executing this tutorial as I write this. Thank you very much!

    I would, however, like to make a correction to the first PHP snippet.

    It appears there should be a ” ‘ ” (apostrophe if it doesn’t render) on the eighth line of the first snippet.


  3. Sudhanshu says:

    Thanks Matt. I’ll just update it.

  4. Hi,

    Is there any API available like APNS, which accepts the .p12 file directly instead of .pem file?

    thanks and regards
    Manoj d

  5. Sudhanshu says:

    No. I’m not aware of any.


Find us on Facebook
Follow us on Github
Track us on Basecamp