How to fix an Azure Notification Hub that is deleting Apple APNS device registrations

ServiceBusI was working on a Hybrid Mobile Enterprise application which uses Azure’s Notification Hub service. This push notification service is based upon the Servicebus functionality of Azure and can be used to send notifications over iOS, Android, Windows and Kindle’s notification network using both their native messaging methods or generic templates, which is very nice (check it out). Our app has mobile clients for iOS and Android and will add the Windows platform in the near-future, so that was a very good fit as you can imagine.

Cool Story, Bro!

Device registration (so that the services know what device to send a message) is handled via a custom implemented API which was also hosted in Azure.

[ad name=”Generic Large Mobile Banner 320 x 100″]

As I compiled and submitted the iOS client and it was approved by Apple we were noticing one strange effect: using Visual Studio’s Server Explorer functionality I could see that there were Apple Devices registered in the Notification Hub that we setup. As soon as we’d send a notification (either via our own web-based test interface, the Server Explorer or the Azure Management Portal) the device registration would be deleted!?

Fuuuuuuuu

Read about the cause of this registration deletion issue and how we fixed it after the break.

TL;DR
If the APNS device’s registration is being deleted while using a Azure Notification Hub whenever you sent a notification to it check that the certificate matches the one used for building your app in the current context (production/sandbox).

[ad name=”Generic Large Mobile Banner 320 x 100″]

Okay, so the notification hub appears to be eating your device registrations. Now what?
The first thing we needed to do was find out if this was being done by the Notification Hub itself, or that it was being triggered from one (or more) of the native push notification services.

Using the Server Explorer functionality in VS2015 we could easily conclude that the deletion only affected the Apple Push notification Service (APNS) and that only those registered devices would be deleted after sending them a test notification.

There was a really strange thing though: Whenever I wanted to debug the app to see the Javascript logging for this app (it IS a Hybrid app, remember?) the app seemed to be fine and just receiving the notifications correctly.

Wait. What?

After reviewing the Xcode project configuration there was only one conclusion possible: Because the app was receiving the notifications whenever I build-n-run it in Debug mode and the registration was being deleted whenever I build-n-run the app using Release or AppStore configurations it just HAD TO BE provisioning profile related. The debug config obviously used another profile for which the Notification Hub was working out fine.

Then I remembered: In the Azure Management Portal the Notification hub needs a profile/certificate (depending on the platform) to be setup for each native service so Azure can hookup with the service. In case of APNS you need to upload a certificate and – since Azure allows to setup a production or testing/sandbox environment from their management console (see this page, step 7 on how to specify that) – the cause for the registration deletion was getting clear…

So, the solution for APNS device registrations that are being deleted (whenever everything else is setup correctly) is very simple:

  • Check that the correct certificate is uploaded for the desired context – being production or sandbox – to ensure it matches the certificate that is being used to build and sign the iOS app client.
  • Whenever the certificate is not equal the device registrations will be based on a wrongly generated device token that will end up in the device registration being deleted.
  • For the sake of being complete, you can setup the certificate for the environment using the steps on this page (especially step 7 that shows how to select production/sandbox)

Awesome

[ad name=”Adsense 200×200 rechts 2″]