Sending Apple Push Notifications in ASP.NET and C# – part 1

In this article series i will cover sending apple push notifications form asp.net application. This will be a detailed article so I will break it down to 5 parts.

Part 1 will cover key concepts about APNS service, how we can use it and its restrictions.

Part 2 will cover generating required certificates for sending push notification.

part 3 will cover installation of certificates on your computer.

part 4 will cover sending push notification using Moon-APNS library.

What is Push Notifications?

One of the widely anticipated features of the new iPhone OS 3.0 is push notifications which allow messages to be sent directly to an individual device relevant to the application that has been installed. Apple has demoed this as useful for news alerts, or IM notifications.

Apple provides detailed code documentation for the iPhone OS code that is needed to implement and handle the alerts on the device but only provides a higher level guide for the provider server side.

As a provider, you need to communicate with the Apple Push Notification Service (APNS) to send the messages that are then pushed to the phone. This is necessary so that the device only needs to maintain 1 connection to the APNS, helping to reduce battery usage.

This tutorial will go into code-level detail about how we built our push notification provider server to allow us to interact with the APNS. Since we develop in asp.net, our examples will be in C#.

Bellow is a sample push notification on iPhone screen:

Sample notification on iPhone screen

Basic Structure for APNS

  1. You connect to the APNS using your unique SSL certificate
  2. Cycle through the messages you want to send
  3. Construct the payload for each message
  4. Disconnect from APNS

The flow of remote-notification data is one-way. The provider composes a notification package that includes the device token for a client application and the payload. The provider sends the notification to APNs which in turn pushes the notification to the device.

Apple documentation

Restrictions

  • The payload is limited to 256 bytes in total – this includes both the actual body message and all of the optional and additional attributes you might wish to send. Push notifications are not designed for large data transfer, only for small alerts. For example we only send a short alert message detailing the server monitoring alert triggered.
  • APNS provides limited status feedback as to whether your message was successfully delivered. One reason for this is that messages are queued to be sent to the device if it is unreachable, however only the last sent message will be queued – overwriting any previously sent but undelivered messages. Only feedback available is for devices Tokens that were previously but are no longer valid, such as if the user has uninstalled your iPhone application and do not receive notifications any more.
  • Push notifications should not be used for critical alerts because the message will only be delivered if the device has internet connectivity.
  • The SSL certificates used to communicate with APNS, discussed below, are generated on an application level. The implementation discussed in this tutorial only concerns a single iPhone application so if you have several, you will need to adapt the code to use the appropriate certificate(s) where necessary.

Device Token

Each push message must be “addressed” to a specific device. This is achieved by using a unique device Token generated by APNS within your iPhone application. Once this token has been retrieved, you need to store it on your server, not within your iPhone application itself. It looks something like this:

c9d4c07c fbbc26d6 ef87a44d 53e16983 1096a5d5 fd825475 56659ddd f715defc

On This Tutorial we will use an open source library called “Moon-APNS” to create notification, send them to apple servers and receive feedback. This library can be downloaded from https://github.com/arashnorouzi/Moon-APNS

Feedback Service

Apple provides a feedback service which you are supposed to occasionally poll. This will provide a list of device Tokens that were previously but are no longer valid, such as if the user has uninstalled your iPhone application. You can then remove the devices Tokens from your database so you do not communicate with an invalid device.

Payload Contents

The payload is formatted in JSON, compliant with the RFC 4627 standard. It consists of several parts:

  • Alert – the text string to display on the device
  • Badge – the integer number to display as a badge by the application icon on the device home screen
  • Sound – the text string of the name of the sound to accompany the display of the message on the device

*This tutorial will only deal with the basics by sending a simple alert text string but this can also be another dictionary containing various options to display custom buttons and the like.

The raw interface

Once an alert is generated within Server Density, the payload is built and then inserted into a queue. This is processed separately so that we can send multiple payloads in one go if necessary.

Apple recommends this method because if you are constantly connecting and disconnecting to send each payload, APNS may block your IP.

As described by Apple:

In Next article we will cover how we can generate required certificates for sending push notifications.

Advertisements