Swift Tutorial: Develop a Push Notification enabled app and .Net Based Push Notification Server - SELISE

Swift Tutorial: Develop a Push Notification enabled app and .Net Based Push Notification Server

December 17, 2014

In the latest addition to our range of tutorials, we will be exploring the realm of push notifications. This tutorial will help you to learn to develop an app that utilises the push notification, and simultaneously, allow you to develop a .Net based push notification server.

Getting smack in the middle of a rather hectic schedule, caused my absence through the series. Now that there is finally some time to breathe, I hope we can reconnect as we venture out to our latest programming adventure.

Our agendas for this rendition of the tutorial series are:

  • Why this particular blog post;
  • What is a notification center and its pros and cons;
  • Before writing the first line of code, what you should configure in the iOS Dev Center to use “Notification Center”
  • What we need to use inside our app to enable the Notification Center
  • Finally, how to develop a Notification Server which serves the notification to the app users with the help of the Apple Notification Center

Sounds good, right? Lets dig into it, then.

Why this particular blog post

There are quite a few examples  online on developing a notification server, so I thought of giving it a go. Most of these examples are based on PHP as a notification server, and this is something not too comfortable for me.  Being a .Net developer, I tried to come up with a notification server that is based on .Net.

We have several apps published, and majority of our services are developed in .Net, which demanded the server to be written inside a .Net environment. After a bit of perusing, I managed to find a number of relevant articles online. However, most of them were pretty complex and included internal errors. My search for a more simpler and lightweight server ended when I stumbled upon MoonAPNS.

There must be other developers who have thought along the same line. This blog is for them 🙂

The Notification Center

Notification centers are very useful, and can be used in a variety of ways to engage the users. Push notifications can be employed for certain reminders (meetings, scheduled activities), or promotions and offers. However, it also comes with its own set of cons:

  • They are not always reliable! There is no guarantee that a push notification will actually be delivered, even if the APNS server accepted them
  • As far as our server is concerned, push notifications are fire-and-forget; there is no way to find out the status of a notification after sending it to APNS
  • User’s iPhone/iPad may not be able to receive push notifications all the time
  • In some cases, it can be expensive.


Before jumping straight into the xCode, there are a few things that need to be done, mainly in Keychain and Dev Center.

So let’s open Keychain. Go to “Keychain Access” –> “Certificate Assistant” –> “Request a Certificate from a certificate authority”. The “Certificate Assistant” window will popup. Enter “User Email Address”, “Common Name” and then click the “Continue” button. Save into disk and give it a name.

Suppose the name of the file in question is: “pushtest.certSigningRequest”

Open “develper.apple.com” and go to “Member Center” and Login.

Go to “Certificates, Identifiers & Profile”. Click “Certificates”. Click “App IDs” in the “Identifiers” section. Click “+” on your top right.

When the menu comes, click an app Name, Bundle ID and most importantly check the  “Push Notification” check box from “App Services” section.


To complete the process, click done. Now let’s go back to “App IDs” and click the app the you just added. You should now see that your app allows for “Push Notification” but it still requires a configuration (Orange blip).

So let’s get down to the configuration. For configuring “Push Notification” on the “Development Mode”, click the “Edit” button. Go to the Push Notification section and click “Create Certificate” from the “Development SSL Certificate” section.

Once you click this button, you should be taken to a “Certificate Signing Request” window where they’ll ask you to create a CSR, which we already did. So, we can just press the “Continue” button. Now click the “Choose File..” button and select the “pushtest.certSigningRequest” file that we generated earlier and hit “Generate”. Go on and download the Certificate too.

Suppose the name is: “pushtest.cer”.

It is crucial to click this file from your download destination and add this with our Keychain. You just have to click once in this file and it should do this automatically. If done properly, you should be seeing this:

We are almost done. We just need to add our provisioning profile so that we can develop the app from xCode. For this go to “Development” from the “Provisioning Profiles” section and click “+” from top right.

Here, remember to check the “iOS App Development” option and proceed to “Continue”. From the “APP ID” dropdown, select the app that we had already worked on and hit “Continue”. Now you can download the provisioning profiles. If we click this downloaded provisioning profile, it will automatically be added with the xCode. So we are now ready to do the sample iOS app to test the push.

What we need to use inside our app to enable Notification Center

This part is very simple. Just open xCode and select “Single View Application”, keeping in mind to select “Swift” as the language.
That is basically it, nothing fancy. We just have to configure the app inside the AppDelegate to accept “Push Notification”. Now you should use the codes below which you require to register the push notification service on your app.

Now, if we run the sample app (basically an empty white screen), it will ask for permission to use the “Push Notification”.

Allow permission and that is all from our client side development.

How to develop a Notification Server which serves the notification to the app users with the help of the Apple Notification Center

Now to the main part of this tutorial. We will assume that we have an app published, for which we require to send the notification from the backend server. This means we will have our server/service where we will have our business logic, and in certain cases we may send the notification to our users as well.
Is that clear? Great.

Step 1

In the “Keychain Access”, select the private key associated with the public key. Press export and give it a name.


The exported key should be a .p12 formatted certificate, which will be used inside our server. The role of our server will be to request a call to Apple Push Server (Sandbox server for development) with this certificate and a Simple structured data (256 bytes).
This data may contain the notification message, badge, and sound filename (please refer to the message structure in the Apple forum for details). This data will be sent by our server to the Apple Push Server, where the magic happens.
What I can’t stress on hard enough is the need to carry out these steps with care, otherwise debugging the whole process may be very difficult and cumbersome.

Step 2

Now that we have our .p12 certificate, it’s time to focus on the server implementation. For this, I will be using MoonAPNS. You can go to this site and download the source files from there.

Step 3

Create your own .net App and reference with Moon-APNS. Write the following:

var payload1 = new MoonAPNS.NotificationPayload("deviceToken", "Hello Push Notification", 1, "default");

var p = new List { payload1 };

var push = new PushNotification(true, ".p12 file name here", "Password");
var rejected = push.SendToApple(p);
foreach (var item in rejected)

You will find the device token in the output window of xcode. Please note that you need a real device to get a device token. Go ahead and use the device token (removing the blank space).
That was it. If you run the code below, you should see a notification like this:

Hope this tutorial was helpful. Please post your queries in the comments section.