Implementing Azure Notification Hubs – Part 1

Introduction 

Sending push notifications to devices is a requirement of most applications. Unfortunately implementing this is not as simple as you would expect. Azure offers a service called Notification Hubs which makes implementing this feature easier, but there is still a fair bit of manual work involved. 

Azure Notification Hubs offers a free tier that supports up to 500 devices in a namespace. Microsoft offers libraries to interact with Notification Hubs for various programming languages, but also offers a REST API, which is what the libraries themselves use. 

Each device ecosystem has its own push notification infrastructure that is developed and supported by its vendor. The ecosystems I’ll be covering are: 

  • Apple (Apple Push Notification System – APNS) 
  • Google Android (Firebase Cloud Messaging – FCM) 
  • Microsoft Windows (Windows Notification Service – WNS) 

Without Notification Hubs we would need to implement integration with each ecosystem and support that integration ourselves. Notification Hubs can simplify the back-end administration by supplying one interface to send a push notification to. Notification Hubs will then take care of routing the push notification to the correct push notification service for the device. We do however have to register with these providers and set the access credentials in the Notification Hub. 

The complete solution for this can be found on my GitHub account.

Planning 

Before we jump into the code, let’s first discuss what exactly we’re going to do, and what we’re not going to do. To keep things simple, the application will not make use of proper authentication such as OAuth 2.0, which is what we would likely use in a real-world application. We will have to identify users, but we will accomplish that by allowing users to set their usernames in the apps, which will then be provided to the API. We’ll trust that users are being honest about their username. Of course, this is completely insecure, but the aim of this project is not to implement a secure login system, but to learn how to implement Azure Notification Hubs. By not complicating the code with other concerns, this approach will make that easier to accomplish. 

We will also be managing the Notification Hub from the backend API. It will need to keep track of devices and users. If a user deletes their account, the API needs to remove their devices from the hub. We will be using tags to send notifications to only a specific user, with the format User_Username

The API will store user and device information in an SQLite database using Entity Framework Core as the ORM. 

The user-flow will be as follows: 

  • A user sets their username on the application. 
  • The user can then choose to sign in. 
  • Once signed in, the user can receive push notifications. 
  • The user can choose to sign out. 
  • In both cases the user would be signed out of the app and would no longer receive push notifications. 
  • If the user has no other devices signed in, the API will delete the user. 
  • When the API receives a sign-up request from the app, it must store the device with the user and then register this device in the Notification Hub. 
  • The API must also keep track of a user’s devices. 
  • If a user signs out, their device must be removed from the Notification Hub. 

Obtaining Credentials  

There is already excellent documentation online for obtaining credentials for all the ecosystems supported by Notification Hubs. This documentation is also kept up to date by Microsoft. Therefore, I won’t be repeating that information here but will instead supply the links to where you can find it. 

If you want to follow along, you do not need to set up every eco-system, although you can. Just set the one up that you have a testing device and developer account for. Please note that iOS requires testing on a real device, not a simulator. 

Create a Notification Hub in the Azure Portal and put all the credentials for the ecosystems under Settings. This will allow you to check if you have the correct credentials as the Azure Portal will not let you save if the credentials are incorrect. 

Create The Backend 

As previously mentioned, the backend will be a .NET Core API. We will be interacting with it by making API calls using Insomnia as an API testing tool. You can also use Postman or another tool. If you want to take things a step further, you could even build a SPA with something like Blazor

Open Visual Studio and select Create a new project. I am using Visual Studio 2022 for these screenshots so they may look slightly different if you are using a different version. 

Search for ASP.NET Core Web API in the list of available templates, select it and click Next

Give the project a name and select a location to save it, then click Next. I called the project LearningNotificationHubs.API and the solution LearningNotificationHubs

Select .NET 6.0 as the Framework, set the Authentication type to None, check Configure for HTTPS, don’t enable Docker and uncheck Enable OpenAPI support. This will provide us with a good starting point without unnecessary clutter that will distract from learning about Notification Hubs. Finally, click Create

Once the solution is ready, click the Run button to build and run the backend. The browser should load automatically, but if it doesn’t, load it up on the URL your application is running on, usually https://localhost:5001/weatherforecast for the starter template. 

Our backend doesn’t do much yet, except for return some hardcoded JSON. 

Create The Apps 

Right-click on the solution in the Solution Explorer, then navigate to Add -> New Project… 

In the list of project types, select Mobile App (Xamarin.Forms). If you cannot find it, try typing Forms in the search box. Then click Next

Give the project a name and click Create. I named my project LearningNotificationHubs.Xam

You’ll then be asked to select a template. Choose the Blank template and all three platforms – Android, iOS and Windows (UWP). 

Run the application on the various platforms to make sure everything is working correctly. You should be presented with a screen like the following: 

Create a Shared Project 

Right-click on the solution in the Solution Explorer, then navigate to Add -> New Project… 

Choose to create a C# Class Library – If you are struggling to find it, type Class in the search box. Don’t select the .NET Framework or Universal Windows options. You need to select the option that targets .NET Standard or .NET Core. Then click Next

Give the project a name. I called mine LearningNotificationHubs.Shared. Then click Next. For the Framework, choose .NET 6, then Create. This project will contain code that is to be shared between the API project and the Xamarin projects. 

You should now have the following projects in your solution: 

Summary 

At this point we have configured the various ecosystems, obtained our access credentials and created a backend API project as well as app projects to work on. In the next posts, we’ll continue to work on both the backend and the apps to implement Azure Notification Hub support.  

Leave a comment

Your email address will not be published.