iOS Push Notifications in C# with Moon-APNS

I have been busy developing backend of an IPhone application which is sending considerable number of push notifications twice a day. Trying to fix some issues and bugs on other free and open source c# push notification libraries I came up with the idea of writing my own c# library and as a big fan of open source I’m going to share the library on GitHub.

The library is called Moon-APNS and it can be downloaded from here.

This is the first version of the library, but It has been tested under pressure and on a production version of an application so feel safe to plug it to you application. Moon-APNS can be used in any .net application web or windows based.

Moon-APNS is benefiting from new Apple Push Notification structure, called Enhanced Push Notification. Which enables the library to receive feedback on each notification sent to apple server asynchronously, you may say we can receive that response with apple feedback service, but unfortunately if you send a payload to apple server with wrong format, broken or missing device tokens apple will terminate the connection straight away and it’s really hard to figure out which one was the faulty payload when you are sending them one after each other because it takes up to 2 seconds for the connection to be closed. Using Enhanced push notification you can sign each payload with a unique identifier and even better you can set TTL on each payload so apple know how long they should try to deliver the push notification before it expires. Moon-APNS will send payloads and receive feedback asynchronously and will re connect and resume sending the queue in case of any errors. All device tokens of rejected payloads will return as a list when the queue is sent and feedback is received from apple with error code sent by apple so you will know why the payload was rejected.

In Moon-APNS I’m using free and open source NLog library to log all events happening behind the scene which makes it really easy to debug the application while you are on production and you can’t attach a debugger.

Sending Apple push notification has never been so easy with Moon-APNS you will need less than 10 lines of code to send push notification and receive the feedback.

Ok enough talking I think it’s time for some coding.

1) You should generate your payloads:

 1:  var payload1 = new NotificationPayload("DeviceToken","Hello !",1,"default");

Feel free to add custom parameters to your payload as bellow:

 1: payload1.AddCustom("CustomKey", "CustomValue");

2) Moon-APNS accepts a list of payloads, so I will add my payloads to a List:

 1: var notificationList = new List {payload1, payload2, payload3};

3) create an instance of Moon-APNS push notification class, and pass true or false for using sandbox, location of your p12 file, and password for thep12 file if you have one and blank string if you don’t have one.

 1: var push = new PushNotification(false, "P12File location","password");

4) Create a list for your feedback, returned from library which will contain rejected payloads list.

5) Saved best for last call SendToApple method and pass list of your payload.

 1: var rejected = push.SendToApple(notificationList);

Is that all? Well yes it is! So if we ignore lines for generating payloads I can say we are sending push notification ad receiving feedback in 2 lines of code. Everything you need to do is handled by Moon-APNS library for you so you can have more time to consider on your application logic.

Feel free to email me or post your questions as a comment and I’ll be more than happy to assist you as much as I can.

This is the first version and I will commit new builds with new features soon.


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

In this article series i will cover sending apple push notifications form 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, 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


  • 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

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.