Understanding Apple Push Notifications (iPhone)

Notification payload uses JSON as data format! (RFC 4627)

iPhone OS is a closed platform. Daemons (background processes) are not allowed. This means your device (iPhone / iPod) cannot run a user service and communicate with backend servers unless your application is running.

Steve Jobs has been quoted as saying: “You don’t want your phone to be an open platform … you need it to work when you need it to work. Cingular/AT&T does not want their west coast network to go down because some application messed up”.  I am not sure how true is that as other platforms have shown otherwise. Bottomline is that lack of background processes (or the restriction for user apps) in iPhone OS seriously limits some app capabilities. This is where Apple push notification service comes in and fills in the gap.

APN (Apple Push Notification) service provides the content provider an ability to invoke an application on the device and/or pass a notification (You can call it “Call for Action”) to it. Though it does not give a device all the capabilities/scenarios that a background process can enable (like prefetching data) but it can surely bridge the gap and enable “Push” messaging scenarios.

Before we understand APNs and how the device, provider and APN Service are bound together, lets pay attention to some terms:

Device: iPhone / iPod
APN Service: Apple’s Push Notification Service that sends notifications to target device(s)
APN: Apple push notifications. APNs are of three types. Sounds, Batches and Text.
Notification Server: Content Provider’s server that sends notifications to Apple’s APN Service to be stored and forwarded to the target device(s)
Token: A unique identifier that is used to uniquely identify a device (It is not the same as Device ID)
X.509 SSL Certificate: Certificate that establishes trust relationship between APN Service and your own notification Server. (APN Service and iPhone already have the trust relationship using a similar SSL Cert)

Please note that “Notification Server” refers to your company’s Content Server (Depending on the need your company may have a single server serving content and notifications or have a multi server deployment) that is tasked with sending notifications to APN Service.

Apple has published extensive docs on APN Service that can be found here.

Lets break the process of enabling push notifications (for your iPhone app) in simple steps (for developers of course):

STEP 1: Setting up the device (Registering the device with with APN Service)
Before you start coding, please ensure you have configured the App ID properly, generated the client SSL Certificate and updated the provisioning profile for the app id (Bundle ID of your iPhone app). Assuming that you or your team lead has already taken care of these steps, you are ready to implement the code patterns.

For your application to listen to push notifications, you need to register with APN Service using “registerForRemoteNotificationTypes” message. The message takes APN types (the types that your app wants to enable) as an argument.

-(void)application:(UIApplication *)application

{

// Register the types that you want to enable

UIRemoteNotificationType enabledTypes = UIRemoteNotificationTypeSounds | UIRemoteNotificationTypeBadges;

[application RegisterForRemoteNotificationTypes:enabledTypes];

}

Once you have registered for notification types that you want to receive, you will receive two delegate callbacks. One in case of success (this will bring the token that uniquely identifies the device with APN Service) and the other in case of failure (this will bring an NSError).

//SUCCESS

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

{

// Your code goes here: get the token and do something

}

//FAILURE

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

{

// this will happen only if  SSL Cert, Bundle Id, Provisioning profile is wrong, fix these issues and all should be fine

}

NOTE: You need to run your app on device to test Notifications. Notifications don’t work on Simulator

Besides these two mandatory delegate callbacks there are some other optional callbacks that you may implement in your code. The most commonly implemented scenario is where a notification is received when your application is already running. In this case, the iPhone OS does not do anything other than calling a callback (if you have implemented it) in your app.

– (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

// This delegate callback is called only if your app is running, “userInfo” brings the notification payload.
// Now that you have  the notification payload, this gives you a unique opportunity to draw your own custom views and do some housekeeping work …

}

One more optional message that can be sent to appdelegate is:

– (UIRemoteNotificationType)enabledRemoteNotificationTypes
This is a message, not a callback and returns all notification types that are enabled for the app

NOTE: This is for Solution Architects: Please be mindful of not using APN Service for any “Guaranteed delivery” scenario as Apple does not guarantee delivery and only the last notification is retained. If you send two notifications to a device back to back and the first one has not been delivered yet, it will be overwritten thereby resulting in a loss of notification.

STEP 2: Share the token with Notification Server (this is “your Server” not APN Service)

Lets recap and revisit the TOKEN. Understanding the TOKEN and where it comes from is crucial to building a three way trust between device, APN Service and your Notification Server. Token is a unique identifier that is assigned to your app via a callback (by iPhone OS which transparently gets it from APN Service).

– (void)application:(UIApplication *) applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token

iPhone OS calls this callback on your app as a result of registration request thereby handing your app a TOKEN (as type NSData). This means your app has successfully registered with APN Service and is ready to receive notifications.

Your app is ready to receive the notifications but that does not mean that notifications are available and your SERVER is READY to send notifications. If your app (on iPhone) has not shared its unique TOKEN with your server, the server has no way of targeting the Notification at your app instance via APN Service. Please note that APN Service is simply a store and forward service that forwards the notification by matching the TOKEN.

So, once the app has successfully registered with APN Service, you need to share this TOKEN with your server. The implementation of how to share the TOKEN is Server specific. Your company may use a REST, SOAP, JSON based interfaces to communicate. What ever the interface, you need to pass on this TOKEN to the Server so that it can target the notification at your app instance.

You can do this in “applicationdidRegisterForRemoteNotificationsWithDeviceToken” callback.

STEP 3: Setting up the TRUST between APN Service and your Notification Server

This step is a server side imperative and will be undertaken by Server side developers. If you are an iPhone developer, you are tasked with implementing the client side functionality. In that case you can assume that server has already been set up for you but if you are one of those unfortunate few who has to manage the certificates, implement the server and write the iPhone app, then you will find rest of the paragraphs useful.

You can implement your server using any Middleware server like Microsoft BizTalk, IBM MQSeries, TIBCO or a custom service written in RUBY, Python, C# or Java. There is a ton of sample code available on Apple dev forums for reference. It is important to understand and accomplish the following:

a). Exchange the SSL Certificates between your Server and APN Service (Please remember SSL Certificates are App specific. Architecture of your Notification Server is totally up to you. You will need to have one SSL Certificate per application)

b). Open a persistant connection with APN Service. It will be a cardinal sin to open/close connections every time a notification is sent to APN Service. Remember threat models and denial of service attacks?

c). Implement a scenario to reopen connections if there is an activity timeout. Android’s service architecture is a very good design pattern that can be used to implement a notification service if you are building a custom service (that acts as a notification server).

d). Implement a Feedback collection service. APN Service has a Feedback service that records events/notifications sent to uninstalled applications. This service can be used in some very interesting ways to know how often the app is being uninstalled.

Advertisements

11 thoughts on “Understanding Apple Push Notifications (iPhone)

  1. sathish says:

    Only one application on an iPhone, iPad, or iPod touch can be active in the foreground at any time. Many applications operate in a time-based or interconnected environment where events of interest to users can occur when the application is not in the foreground. Local and push notifications allow these applications to notify their users when these events occur.

  2. Siddhesh Naik says:

    We can remove notifications for an application. We can do so by using Settings>Notifications and removing all notifications for that application.
    Also, as it is a push based service, a notification is only send to the application when there is some new data available. This means that, the application does not need to connect to the server on a regular basis to check for updates. This makes it power efficient, as no energy is spent in contacting the server on a regular bases and having a daemons running in the back ground.

  3. Ravi Joshi says:

    APN Service allows the provider of some service to send notifications to the registered client devices (iphone, ipod touch) even when the application is not running. This optimizes the phone for battery life. Also the size of these notifications is not an issue because it is allowed to carry a payload of max size 256 Bytes. There is a feedback service provided by the device to the APN service provider to make the provider aware that the user has uninstalled the application from his device.
    The payload in this example specifies that device should display an alert message with both Close and View buttons. It also request that the application icon be badged with 9 and that a bundled alert sound be played when the notification is delivered.

    {
    “aps” : {
    “alert” : “You got your emails.”,
    “badge” : 9,
    “sound” : “chime”
    },
    “acme1” : “bar”,
    “acme2” : 42
    }

  4. Yatin says:

    Security Architecture in short:

    Connection trusts between APNS and Device/Provider is established by exchanging each others certificates after the TLS initialization and then validating them. Successful validation at both ends results in establishing a TLS connection.

    An application needs to register to receive push notifications. After receiving such a request from an application, iOS forwards it to the APNS.

    Token generation is handled by APNS and are used to ensure notification delivery to the intended device only.

    It generates a token based on the device’s certificate, then decrypts and sends this token to the device. The device then shares this token with the Provider. This way APNS ensures that all tokens used are generated by itself only.

  5. Venkatesh S.R says:

    The APN service provides the content provider an ability to invoke an application on the device and/or pass a notification. This means that, the application does not need to connect to the server on a regular basis to check for updates.This means that, the application does not need to connect to the server on a regular basis to check for updates.

  6. Vidhya says:

    The third parties can now send messages to the APN that in turn pushes it to the phone. And you get an update on a app or about the app.It just establishes a secure connection with the APN service using the SSL. The messages are sent with a constraint on the payload.

  7. rahul parate says:

    Hi,
    i want to develope a windows service to send push notification to iPhone is there any sample project or code available to do so.

  8. Hi, Neat post. There’s an issue with your web site in web explorer, might test this? IE still is the market chief and a huge portion of folks will omit your great writing because of this problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s