Send push notifications to iPhone with PushSharp, C#, MonoTouch, client-side and server-side

Lots of Mobile apps need push notification functionality. A push notification is a notification that is sent by Apple to a mobile device. You can send these notifications to the Apple notification service (APNS), which in turn sends it to a mobile device. Every device has it’s own Device Token so Apple knows where to send the notification. In this post I’m going to show you how I use PushSharp to send Push Notifications to iPhone using C#, MonoTouch (Xamarin.iOS), client-side and server-side.

In this post I’m only talking about Apple Push notifications.

I make use of the following library, PushSharp (formerly APNS-Sharp) because it’s very easy to implement. The difficult part is creating the certificate and making sure your app is registered with Apple for sending push notifications.

PushSharp-Diagram
PushSharp is a server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), Android (C2DM and GCM – Google Cloud Message), Windows Phone, and Windows 8 devices.

Client-side

Note: To copy the source code, just double-click on the code and do [Ctrl-C]

AppDelegate.cs

DeviceToken.cs

Server-side

PushNotificationApple.cs

PushNotificationService.cs

Configuration

To be able to call PushSharp in your server-side solution add references to the following dlls:

Newtonsoft.Json.dll
PushSharp.Apple.dll
PushSharp.Core.dll

The easiest way to do this is by adding the NuGet Package “PushSharp” to your solution with the NuGet Package Manager.

Another way to do this is:
- Download the zip file containing PushSharp from https://github.com/Redth/PushSharp
- Open the solution : \Downloads\PushSharp-master\PushSharp-master\PushSharp.sln
- Build the solution.
- Take the 3 dlls from
\Downloads\PushSharp-master\PushSharp-master\PushSharp.Apple\bin\Debug
- Add them as references to your solution.

Set up your Apple app to be enabled for sending push notifications

Before you’re able to use the above code, you still have to take a few steps.

Follow this guide:

https://github.com/Redth/PushSharp/wiki/How-to-Configure-&-Send-Apple-Push-Notifications-using-PushSharp

This is a step by step Picture guide that will show you how to set up your Apple App to be enabled for sending push notifications, create the proper certificates for sending push notifications, and how to configure your PushSharp code to send out push notifications.

There is also a very elaborate guide, and even though this is for Objective C, it explains the whole process very well:
Apple Push Notification Services Tutorial: Part 1/2:
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
Apple Push Notification Services Tutorial: Part 2/2:
http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2

And then there is also this guide I found:

https://www.parse.com/tutorials/ios-push-notifications

This also explains the certification stuff very well, it shows how to go through the process using the newest version of the Apple Developer Certificates / Profiles portal. You’ll have to follow this up until step 2. The .p12 certificate you exported in Step 1: “Export Apple Development IOS Push Services:…”, and save it as a .p12 file” will be used in the service you created to send Push Notifications with PushSharp.
There is also a step 8: “Troubleshooting Tips: that is very helpful.

Sending the push notification

In your server-side solution at the place where you want to send a push notification to the Mobile device, call the PushNotificationService like this:

 

Also, you need a way to let the PushNotificationService know the devicetoken of the mobile device.
This is done in the app by calling the service and doing a call to

DeviceToken.RetrieveToken();

and passing the result to the service.

———–