Programming Apple Push Notification Services

One of the key limitations of the iPhone is its constraint on running applications in the background. Because of this, applications cannot keep running in the background when the user switches to another application. So, applications that require a constant state of connectivity (such as social networking applications) will not be able to receive timely updates.

To remedy this limitation, Apple released the Apple Push Notification Service (APNs). The APNs is a service that allows your device to be constantly connected to Apple’s push notification server. When you want to send a push notification to an application installed on the users’ devices, you (the provider) can contact the APNs so that it can deliver a push message to the particular application installed on the intended device.

In this article, you will learn how to perform the various steps needed to create an iPhone application that uses the APNs.

Generating a Certificate Request

The first step to using the APNs is to generate a certificate request file so that you can use it to request for a development SSL certificate later on.

6. The SSL Certificate that you download is named aps.developer.identity.cer. Double-click on it to install it in the Keychain Access application (see Figure 14). The SSL certificate will be used by your provider application so that it can contact the APNs to send push notifications to your applications.

Creating a Provisioning Profile

The next step is to create a provisioning profile so that your application can be installed onto a real device.

1. Back in the iPhone Development Program Portal, click on the Provisioning tab and click on the New Profile button (see Figure 15).

Figure 15. Selecting the Provisioning tab

2. Type in MyDevicesProfile as the profile name. Select PushAppID as the App ID. Finally, check all the devices that you want to provision (you can register these devices with the iPhone Developer Program Portal through the Devices tab). Click Submit (see Figure 16).

Figure 16. Creating a new provisioning profile

3. The provisioning profile will now be pending approval. After a while, you will see it appear. Click on the Download button to download the provisioning profile (see Figure 17).

Figure 17. Pending the approval of the provisioning profile

4. The downloaded provisioning profile is named MyDevicesProfile.mobileprovision.

Provisioning a Device

With the provision profile created, you will now install it onto a real device.

1. Connect your iPhone or iPod Touch to your Mac.

2. Drag and drop the downloaded MyDevicesProfile.mobileprovision file onto the Xcode icon on the Dock.

3. Launch the Organizer application from within Xcode and select the device currently connected to your Mac. You should see the MyDevicesProfile installed on the device (see Figure 18).

Figure 18. Viewing the installed provisioning profile

Creating the iPhone Application

1. In Xcode, create a new View-Based Application project and name it as ApplePushNotification.

2. Drag and drop a WAV file (shown as beep.wav in this example) onto the Resources folder in Xcode (see Figure 19).

Figure 19. Adding a WAV file to the project

3. Expand on the Targets item in Xcode and select the ApplePushNotification item. Press Command-I. In the Info window, click the Properties tab (see Figure 20).

Figure 20. Entering the App ID for the application

In the Identifier textbox, type <net.learn2develop.MyPushApp.

4. Click on the Build tab and type “Code Signing” in the search box. In the Any iPhone OS Device item, select the profile as shown in Figure 21:

Figure 21. Selecting the profile for code signing

5. In the ApplePushNotificationAppDelegate.m file, type the following code in bold:

6. Press Command-R to test the application on a real device. Press Shift-Command-R in Xcode to display the Debugger Console window. Observe carefully the device token that is printed (see Figure 22). In the figure below, the token is: 38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7c e90d56e9 fe145bcc 6c2c594b. Record down this device token (you might want to cut and paste it into a text file).

Figure 22. Viewing the device token for push notification

7. If you go to the Settings application on your iPhone/iPod Touch, you will notice that you now have the Notifications item (see Figure 23).

Figure 23. Viewing the Notifications item in the Settings application

Creating the Push Notification Provider

A Push Notification provider is an application written by the application’s developer to send push notifications to the iPhone application through the APNs.

Here are the basic steps to send push notifications to your applications via the Apple Push Notification Service (APNs):1. Communicate with the APNs using the SSL certificate you have created earlier.2. Construct the payload for the message you want to send.3. Send the push notification containing the payload to the APNs.

The APNs is a stream TCP socket that your provider can communicate using a SSL secured communication channel. You send the push notification (containing the payload) as a binary stream. Once connected to the APNs, you should maintain the connection and send as many push notifications as you want within the duration of the connection.

Tip: Refrain from opening and closing the connections to the APNs for each push notification that you want to send. Rapid opening and closing of connections to the APNs will be deemed as a Denial-of-Service (DOS) attack and may prevent your provider from sending push notifications to your applications.

The format of a push notification message looks like Figure 24 (figure from Apple’s documentation):

Figure 24. Format of a push notification message

For more details, please refer to Apple Push Notification Service Programming Guide.
The payload is a JSON formatted string (maximum 256 bytes) carrying the information you want to send to your application. An example of a payload looks like this:

4. Press Command-R to test the application. You will be asked to grant access to the certificate. Click Always Allow (see Figure 26):

Figure 26. Granting access to the SSL certificate

On the iPhone/iPod Touch, ensure that the ApplePushNotification application is not running. To send a message to the device, click the Push button. The server essentially sends the following message to the Apple Push Notification server:

1

2

3

4

5

6

7

8

{

"aps":{

"alert":"You got a new message!",

"badge":5,

"sound":"beep.wav"},

"acme1":"bar",

"acme2":42

}

5. If the message is pushed correctly, you should see the notification as shown in Figure 27.

Figure 27. Receiving a Push Notification message

6. If you now debug the ApplePushNotification application by pressing Command-R and send a push message from the PushMeBaby application, the Debugger Console window will display the following outputs:

Summary

In this article, you have seen the various steps required to build an iPhone application that utilizes Apple's Push Notification service. I am interested to know how you are using the APNs for your application. Use the comment feature below to share with us!

35 Comments

Thnx for the cool tut. I got everything to work up until the point where i test it for the actual push from PushMeBaby app “yes I copied in my Device Token” but to no avail any idea what i may be doing wrong? Thnx for the help!!

We are sending alerts to iPad app through apple sand box. From our C# console, it connects to APNS (TCP/IP), authenticates SSL and sends notification. But the device didn’t receive anything. Certificate(.pem) generated for development environment and we are sending the notification in ByteArray format. Is there a way to validate the device token?. Also how to retrieve the status through enhanced notification format. Any feedback/suggession would be of great help. Thank you

Just like others who met the problem, I try to resolve the problem as googler’s instructions:
1. Configuration the App ID with push notification.
2. After that generat a new Provisioning Profile with that App ID.
3. Removed all old provisioning profile from Xcode and iOS device before installing the new one.
4. Last, run test application as you gave, then I met that problem. I can’t get device token.

Although I know error code 3000 means app id and profile are not matched and I’m sure that the profile I used is exactly matched the app id that has already push notification available, I still meet this problem.

Just like others who met the problem, I try to resolve the problem as googler’s instructions:
1. Configuration the App ID with push notification.
2. After that generat a new Provisioning Profile with that App ID.
3. Removed all old provisioning profile from Xcode and iOS device before installing the new one.
4. Last, run test application as you gave, then I met that problem. I can’t get device token.

Although I know error code 3000 means app id and profile are not matched and I’m sure that the profile I used is exactly matched the app id that has already push notification available, I still meet this problem.

Appreciate for your any help.
Thanks.[/quote]

The problem has been solved:
1. Add Entitlements.plist in Resources folder.
2. Add a new key: aps-environment, value: development. Do not modify other key/value pairs .

even I change the port :
[code]SSLSetPeerDomainName(context, “gateway.sandbox.push.apple.com”, 30);[/code]
to
[code]SSLSetPeerDomainName(context, “gateway.sandbox.push.apple.com”, 22);[/code]

but nothing happened !!!!! even my iphone push app outputs , did not change to something like this :
[quote]6. If you now debug the ApplePushNotification application by pressing Command-R and send a push message from the PushMeBaby application, the Debugger Console window will display the following outputs:[/quote]

article looks great, though I still haven’t tried myself :). Before that, I would like to have some experts advice on what I have in mind (I am quite new to this):

– I am thinking of an app able to run on the background and show pop up messages every x hours (i.e. 1 hour started says Hi!, after 1 more it says “How are you?” and so on… of course what I have in mind is a bit more interesting…

The idea being:
– This messages should be already preloaded in the app
– Therefore no need for connectivity from internet

For what I understand from the APN service (very well explained here) is:
– It is meant to be for server app sending notifications to the devices (client apps)
– Therefore need for connectivity

Would appreciate a lot if anybody can give me some ideas on how this can be done and whether this APN is the only option.

An AdHoc profile is NOT!!!! a developer profile (It seems to be seen as productiv)!
Change nothing then the provision file from AdHoc to Develop (take care, the device gets a new token!) and everything will work proper.

This is a website of Afilias Technologies Ltd, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland