Azure Service Bus Notification Hub for iOS + Bonus Xamarin Binding!

UPDATE: Xamarin now has a component in their component store for Azure Messaging, that uses the Notification Hubs described in this blogpost.

Recently I’ve been working with Edwin van Wijk on the brand new Azure Service Bus Notification Hub by Microsoft. It’s a unified messaging hub for delivering push notifications to mobile devices. It’s became GA in August 2013, and now supports all major mobile platforms, including iOS and Android.

Here’s a video of our presentation on this topic, at our weekly tech meetup at Info Support HQ (it’s in Dutch):

The service is quite nice, and is a cloud based contenter to products like PushSharp. You won’t need custom hosts for the integration, which is a benefit over PushSharp and the likes. However, in its current (Preview) version, it doesn’t support nearly as many platforms as PushSharp.

The Notification Hub builds upon the Azure Service Bus infrastructure for relaying messages in a massively scalable way. This means that it also leverages Service Bus’s topics and subscriptions feature for easily grouping messages using Tags. A nice way to filter messages and distinguish groups.

Edwin and I did a session on this. You can find the slides here. The slides are in Dutch by the way.

For iOS, Microsoft has released a native Objective-C library as part of their Azure SDK. There’s a tutorial on MSDN that nicely describes the steps you need to take to consume the library in XCode.

In order to test it, you’ll need to set up quite a lot of prerequisites, including a Windows Azure Service Bus instance, a Notification Hub, Apple AppID and an APNS certificate. A bit finicky to set up, but once it runs, it works nicely.

Xamarin.iOS binding
It’s quite logical for Microsoft to release an Objective-C library at first. Still, I felt a bit dissatisfied having to write my app in Objective-C. I have been wanting to experiment a bit more with creating a C# binding to Objective-C libraries, so I’ve decided to make one available for this library.

I have a working version up on GitHub, which includes a sample. It’s based on the latest API version – you’ll need a Notification Hub created after April 2013 for it to work. You’ll also need to setup your own certificates and fill in the Service Bus namespace, Notification Hub name and Shared Access Secret Listen key.

Basically, you’ll use the library in the RegisteredForRemoteNotifications method on your AppDelegate class. I have a write-up on that in an older blogpost here.

The current binding is a basic one. Completion handlers are mapped to delegates, and I think some of the type mappings could be more C# friendly. I’d also like to make it async/await friendly but I’ll have to experiment with that a bit more.

I’d love to hear your feedback on this!

[UPDATE August 13th, 2013: I’ve updated the Xamarin.iOS binding to the GA version!]


7 thoughts on “Azure Service Bus Notification Hub for iOS + Bonus Xamarin Binding!”

  1. Hallo Roy,

    Ik heb je binding gebruikt om zelf ook een PoC met een MS Notification Hub op te zetten en dat ziet er allemaal goed uit: custom sounds, alerts, en badges werken goed als ik de client app geopend heb.

    Als ik de client app volledig gesloten heb krijg ik een alert en (zoals verwacht) het default notification geluid maar het badge nummer wordt niet gezet.

    Ik gebruik de template opzet die jij in je voorbeeld app gebruikt en die ook door MS wordt uitgelegd. Ik heb alles nagekeken en ook in het template %(badgeValue) staan ipv $(badgeValue) zodat het template de waarde als een int moet behandelen en niet een string.

    Heb jij nog een suggestie waaraan het kan liggen dat de badge value niet wordt gewijzigd zodra ik de notificatie heb ontvangen?

    alvast dank,

    Edwin – EeKay – Klesman

    1. Hi Edwin,

      If you don’t mind, I’ll answer this in English… Cool, thanks for trying the binding, glad it works. One of these days I’ll have a look and see if it needs updating, since it was based on a preview release of the library.

      There are a couple of reasons I can think of why the badge value would fail to update… Did you include UIRemoteNotificationType.Badge in your call to UIApplication.SharedApplication.RegisterForRemoteNotificationTypes? Otherwise iOS will ignore badge value in the notification when it’s handled in the background. Or maybe the notification template is wrong? It should contain the key “badge”, followed by the value, indeed in integer format as you’ve indicated.

      There could also be a bug in my template code in this post… I manually set the badge value only if processNotification is called while the app is active (fromFinishedLaunching == false). Maybe you need to also call it when called from the background (fromFinishedLaunching == true).

      Are you testing on the device or in the simulator? I’ve seen some remarks online that the iPhone 5 simulator wouldn’t update badges. Also, could it be that you’ve disabled badges in Notification Center settings?

      1. Hey Roy,

        I registered for the badgetype (i triple checked) and the template has the “badge” item in it with % in front.

        My assumption was that the function processNotification will be called when the app is active or sleeping in the multitasking modus, but when the app is entirely closed the OS handles the notification (hence the default sound instead of the customized one). I thought that this is also valid for the badge number (so it will be set by the OS to the app)?

        I tried putting the badge code outside the if statement (thus always setting the badge no matter if the function was called from the FinishedLoading or not).

        Last but not least I always test on the device because the simulator indeed has some weird behaviour regarding notfications (I use Spark Inspector to test – local – notifications in my app, great mac dev app btw).

        I am beginning to get the sence that either MS’ Azure code is wrong and that the badge integer value isn’t passed through correctly to the app as an integer. I noticed that i DID receive the badge value when the app was sleeping or active even when it was specified as ..”badge”:”$(badgeValue)” instead of with a % upfront…

        I’ll try and see if the badge DOES work when i wrap things up in a native notification.. If so that might pin-point the issue some more..

      2. I’ve updated my binding to the final version from MS yesterday, since the Notification Hub went live this month. The API changed quite a bit again (became simpler, there are only Register and Unregister methods left on SBNotificationHub), but maybe you could try to upgrade.

        If you have an older Notification Hub running in Azure you might have to create a new one for that as well though, since the API version is different. But you’ll find out soon enough if you get errors using the new library.

        BTW, I just noticed in your example that you have in your template:

        "badge": "%(BadgeValue)"

        Try changing that to

        "badge": %(BadgeValue) – so loose the quotes around the badge value.

        See Apple docs for payload description.

  2. Good day Roy, leaving the parenthesis “” made the template registration call invalid so i’ll be checking the new binding and calls today. I’ll update you on the outcome.

    Where would the world be without badge updates 😉

  3. Hey Roy, I updated my project with the new wrapper and indeed it is a lot cleaner/simpler to use.
    Unfortunately it doesn’t fix the badge number update when the application is not running (in the background or as active app).

    The website gives an example with the parenthesis around the %(BadgeProperty) and it won’t register unless i put them around it. I’ve posted a reply on Scott Guthrie’s announcement (titled “windows-azure-general-availability-of-sql-server-always-on-support-and-notification-hubs-autoscale-improvements-more”) regarding notification hubs and send a mail to him so I hope MS can clear things out.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s