Apple Push Notification Service is a service for propagating information to devices such as iPhone and iPod touch devices. Each iPhone establishes an IP connection with the service and  receives notifications over this connection. We have three Parties here.

  1. service provider
  2. Apple Push Notification Server
  3. iPhone / iPod Touch

A high level simplistic view of the Push notification can be understood better from the following image:

remote_notif_simple.jpg

Here the provider refers to the service provider. Every communication that Provider wants to make to the client Apps can be made only through the APNs.

1. To send a push notification to the client app, the Provider communicates with the APNs .

2. The APNs sends this info to the target device ( which has been subscribed for the Push notification services) and OS takes care notifying the application of the new info sent by the Provider.

As a whole the whole system would look this way as depicted in the following image:

remote_notif_multiple.jpg

There would be multiple providers and multiple devices involved. A single device may receive notifications for many apps and a single provider may sent notifications to many devices.

Quality of Service

When APNs try to deliver a notification to a device, if the device is offline the APNs store the notification and tries to notify the device when it is connected. The notification is retained only for a certain time(Exact time is not specified). By QOS, only one notification for a device is retained.

The Notification Payload

Each notification carries a payload with it and the maximum size allowed is 256 bytes. It is good to avoid info that is confidential in nature  and info that is critical in nature, because this service is a does not guarantee of delivery of notifications.

Each notification must be of a JSON object(a dictionary). This dictionary contains another dictionary identified by the keyword  “aps”. The “aps”  dictionary contains one or more properties that specify the following actions:

  • An alert message to display to the user-

key – alert    value -string or  dictionary

  • A number to badge the application icon with

key -badge   value -number

  • A sound to play

key -sound value -string

Although your notification can have alerts, sound and badge , most users would not like to have all three. so make your choice sensibly.

How do you Register for Push Notification?

Only when an iPhone App registers for a notification the notifications would be sent by the provider. It involves  three stages.

1. The application calls the registerForRemoteNotificationTypes: method of UIApplication. This starts the registration process. It is here you specify the notification type -

some thing like this…

- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here….
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

2.The Application implements the application:didRegisterForRemoteNotificationsWithDeviceToken: method of UIApplicationDelegate to receive the device token.

If registration is successful, APNs returns a device token to the device and iPhone OS passes the token to the application delegate through this method. Registration failure must be            handled using the application:didFailToRegisterForRemoteNotificationsWithError: method.

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@”Error in registration. Error: %@”, err);
}

3. Then application has to send the device token to the service provider as non-obejct binary value. Never cache a device token and give that to your provider.

So thats all with this article. bye….

UPDATE: It is not possible to test APNs functionalities with the simulator. only on a hardware device you can do it.

About these ads