Skip to main content

Azure Notification Hub (Day 23 of 31)

List of all posts from this series:

Short Description
Azure Notification Hub allow us to send notifications from any backend (hosted on cloud or on-premises) to our devices/clients.
I don’t know I you had the opportunity to work on a system that needs to work with different PNS providers (iOS, Android, WP), but managing all different platforms, tokens, trigger notification for each device of group of devices can be a time consuming task not only for developers but also for the person that trigger the notification.
For use cases like this Azure Notification Hub can become our best friend.

Main Features
Platform Agnostic
We can send notifications to any kind of device or system. From Android to iOS, to desktop application or even kindles.
Focus on content
Azure Notification Hubs enable us to focus on the content that we want to send to our applications and not on the notification system platform that we use. In this way we don’t care about the infrastructure and format of the notification that is send.
With only one API call we can trigger a notification that is send to millions of devices across the glob.
This service has the capability to track notification. In this way, we ca know what is the status of each notification that was send, how many devices received it and so on.
Cross Platform
It has support for multiple platforms like iOS, Android, WP, desktop and even kindle.
No Integration development
There is no need to write custom code that integrates each PNS provider.
 Xamarin Support
There is full support for Xamarin cross-platform applications.
Support for Registration from Device or from Backend
Both registration mechanism are supported in the current architecture of Azure Notification Hub. It is recommended to use the second one, because it allow us to update the tags (user subscriptions) on all devices that are registered for that user, not only on devices that are online.
To be able to send a notification to a specific device, tags needs to be used. Each device can have associated one or more tags. When a notification is send it is send to a specific tag. All devices that are registered with that specific tag will receive the notification.
We can see tags as a mechanism to group devices, but in in the same time we can have a tag that unique identify a registered device.
Tag Expression
We can send a notification that target a combination of tags. For this use causes we can define an expression based on AND, OR and NOT. For example we can send a notification for all people from UK and Germany that are interested to receives notification about Christmas.
(( UK || Germany ) && Christmas)

Device Targeting  
As we can saw above when we send a notification we can target a specific tag or a tag expression. There 3 ways to target a notification:

  • Tags
  • Tag Expression
  • Broadcast (all devices that are registered in Notification Hub will receive the notification)

Because each device has a different notification platform with different functionalities, we need to a mechanism to specify different content based on device type and features. For example we may want to send a graphical image as a notification for devices that support this.
Templates allow us to specify a parameterized template for each device type. In this way the final content that will be received by a specific device will contain content that is supported by him.
This is a very powerful feature that you should use. More information about this can be found here
The base mechanism that is used to manage access of different systems (that produce and send notification over the wire) is Shared Access Signature (SAS). Using SAS we can manage access of each system.
SAS is based on a token that can specify what kind of claims has each system. Below you can find the list of claims:

  • Listen (Create, Read, Delete Registrations)
  • Send (Send Notifications)
  • Manage (Create, Read, Update, Delete Notification Hubs, Read registration by tag)

We are allowed to register a notification that will be send at a specific time in future. In this way we can send notifications to our clients at a specific time.
We can export or import a bulk of registrations. In this way we can automatically manage the devices that are registered.

There are some limitation related to notification size, tag name and so on, but this are not real limitations.
For the Free and Basic tier there is also a limitation at the number of tags, devices registered with the same tag and so on. All this limitation are removed if you are using Standard tier that allow us to nock yourself out.

Applicable Use Cases 
Below you can find some use cases when I would use Azure Notification Hub
Discounts notification
If you have a store and every day or week you have special discounts, that you can use this notification system to notify users about special offers and discounts. Combined with scheduler support, you can setup your discounts offers in advance.
Breaking news
For application that share content and news to clients, a system like this can be used with success to send breaking news notification to users.
Notification Hub can be used by applications that needs to send alerts to users when something is happening in their region. For example alerts can be used to send notifications when the risk level in a specific region was changed or a natural calamity will happen.

Code Sample 
// Send notification at a specific time
Notification notification = new AppleNotification("{\"aps\":{\"alert\":\"Special discount ony for you!\"}}");
var scheduled = await hub.ScheduleNotificationAsync(notification, new DateTime(2014, 12, 24, 10, 0, 0));

Pros and Cons 

  • Scalable
  • Template support allow us to use features that are specific for each platform
  • Simple to use
  • Telemetry support
  • Free tier is great for start-ups and is FREE.


When you calculate the cost of Azure Notification Hub you should take into account the following:

  • Number of notification that will be send per month
  • Number of tags
  • Number of devices registered with the same tag
  • Scheduler support

Azure Notification Hub is design to any type of application and customer. From the small one that has a limited budget to the large one that needs strong SLAs and features like import/export. This is a service that you should keep in mind and use it when you need to send notifications.


Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…