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.
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.
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.. 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!