Azure Mobile Services or Notification Hubs
When your dealing with Azure and mobile applications there is a lot of options and capabilities within the Azure Platform. So much so that it can easily get confusing and overwhelming. Also over the last two years Azure has changed so much that it’s almost impossible to find up to date documentation or information.
One area where this is really bad is with Azure Mobile Services. When it launched, it was the end all for a cloud backend for mobile applications. Which this actually is kinda true, it’s very confusing to us that already have backends how we can use Azure Mobile Services for push notifications. It was during the development of Resgrid a company that a partner (staxmanade) and myself founded in late 2012 to provide cloud based logistics and management tools to fist responder organizations (volunteer and career fire departments, EMS, search and rescue, HAZMAT, public safety, etc) that we ran into issues.
Azure Mobile Services allows you to add a RESTful backend to your mobile applications. Say you don’t want to write and host a backend database to store user data, handle logins in your app, process server side requests or perform push notifications, Azure Mobile Services is for you.
Code in Azure Mobile Services is all Node.js based, this makes it super simple to write backend logic. The API’s and Data Tables you create are all REST based with the standard verbs (GET, POST, PUT, DELETE and PATCH). There are also SDK’s out there for multiple platforms to make using Mobile Services super simple with Android, iOS and .Net applications.
In Resgrid we wanted a simple push notification framework to use. We started using PushSharp but quickly decided that we didn’t want to house the push notification logic, it just felt like push notifications and all the work that goes into them should be something that we can farm off to to a 3rd party then we heard about Mobile Services and thought that all of our problems were solved. We were wrong.
It’s very difficult to call Mobile Services to send out just push notifications from a backend service. You can create an Mobile Service API which you can then write Node.js in say the GET verb to then call the push code, but this seems by just a super hacky way to do it. After a little research we found Notification Hubs, this is just what we needed.
Azure Notification Hubs are hidden under the Azure Service Bus. So you first need to create a service bus, then in the notification hubs tab create a new notification hub. In your new notification hub, you can then add the credentials for Microsoft Notifications, GCM and Apple in the configuration tab. In your backend service project Nuget in the WindowsAzure.ServiceBus package to interact with the Notification Hub through code.
Now you can use the NotificaitonHubClient API to interact with Azure, to register your devices to pushes, deregister them and push out native or template notifications.
To register a device for push notifications we use the below code:
var hubClient = NotificationHubClient.CreateClientFromConnectionString("AzureNotificationHub_FullConnectionString", "NotificationHubName"); if (pushUri.PlatformType == (int) Platforms online levitra.Windows8 || pushUri.PlatformType == (int) Platforms.WindowsPhone7 || pushUri.PlatformType == (int) Platforms.WindowsPhone8) { await hubClient.CreateWindowsNativeRegistrationAsync(pushUri.PushLocation, new string[] { “tag1”,”tag2” }); } else if (pushUri.PlatformType == (int) Platforms.Android) { await hubClient.CreateGcmNativeRegistrationAsync(pushUri.DeviceId, new string[] {“tag1”,”tag2”}); } else if (pushUri.PlatformType == (int) Platforms.iPad || pushUri.PlatformType == (int) Platforms.iPhone) { await hubClient.CreateAppleNativeRegistrationAsync(pushUri.DeviceId, new string[] {“tag1”,”tag2”}); }
To send out an Android notification we use the code below:
var hubClient = NotificationHubClient.CreateClientFromConnectionString("AzureNotificationHub_FullConnectionString", "NotificationHubName"); string androidNotification = "{ \"data\" : {\"message\":\"" + title + "\"}}"; var androidOutcome = await hubClient.SendGcmNativeNotificationAsync(androidNotification, "tag1"); return androidOutcome.State;
Tags allow you to filter who you want to get a push notification. This could anything you want to filter by, for example a customer id, or group id, maybe a type. For Android the common template used in examples on the Azure forums looks like this:
"{ \"data\" : {\"msg\":\"Hello for Azure!\"}}"
Notice the “msg” in the JSON, this will cause your Cordova\PhoneGap application to crash sometimes. Change “msg” to “message” and it will work.
"{ \"data\" : {\"message\":\"Hello from Azure, without the crash!\"}}"
So that’s a quick intro to Mobile Services and Notification Hubs. Use Mobile Service if you want a complete backend for your mobile applications in Azure, and use Notification Hubs to integrate push notifications into your existing backend, even if it’s not running on Azure itself.