iOS Push Notifications in C# with apns-sharp

I just recently set up push notification in an iOS application for the first time. The back-end is implemented in C# and searching the web for existing solutions i came across “apns-sharp” (http://code.google.com/p/apns-sharp/). “apns” is short for Apple Push Notification Service and the library, as you would expect, provides a C# api for sending iOS push notifications. The library also contains an api for Apple’s Feedback service for – a service used to retrieve push tokens that are no longer valid, so as to be able to clean up subscribtions.

Initially i got things working on my dev setup without hardly any effort. I then proceeded to publish the project to by budget webhost http://www.web10.dk and suddenly I wasn’t receiving notifications anymore. It took me quite some time to figure out what was wrong…

I was only able to reproduce the error sporadically on my local machine, manifested as a failure to send the very first notification. As there was no way to attach a debugger to the remote host, the tests that led me to the right place wasn’t exactly scientific, but I was able to gather that the connection was beeing closed prematurely, before the notification was actualy send. In the end I found the method below with it’s very descriptive comment.

/// <summary>
/// Closes the Apns Connection but first waits for all Queued Notifications to be sent.  This will cause QueueNotification to always return false after this method is called.
/// </summary>
public void Close()
{
	accepting = false;
			
//Sleep here to prevent a race condition
// in which a notification could be queued while the worker thread
// is sleeping after its loop, but if we set closing true within that 100 ms,
// the queued notifications during that time would not get dequeued as the loop
// would exit due to closing = true;
// 250 ms should be ample time for the loop to dequeue any remaining notifications
// after we stopped accepting above
Thread.Sleep(250);
			
closing = true;

//Wait for buffer to be flushed out
if (workerThread != null && workerThread.IsAlive)
	workerThread.Join();
}

The quick and dirty fix was to up the wait to arround a second. I’m not exactly sure why things were slower on the remote host, but that just goes to substantialize the point, that you shouldn’t rely on timing for things like this.

Event though i realy dislike this part of the design I have to admit it wasn’t enough to put me off using apns-sharp. The library is extremely easy to use and is design to support queueing of messages as well as multiple connections to Apple for speedy delivery.

Ps. Writing this post i decided to have a further look at the loop mentioned in the comment and i came across this:

Thread.Sleep(500);

An issue was allready opened on this, so I just added this additional info 🙂

Tags: , , , ,

2 Responses to “iOS Push Notifications in C# with apns-sharp”

  1. Steve c Says:

    WOHOO! It works! Thanks for saving my sanity!

Leave a reply to Olav Rask Cancel reply