Azure Notification Hubs: Key features and use cases

Azure Notification Hubs: Key features and use cases

Azure Notification Hubs is a highly scalable mobile push notification engine by Microsoft that is compatible with all significant platforms like  

  • APNs (Apple Push Notification Service)  
  • GCM (Google Cloud Messaging)  
  • WNS (Windows Push Notification Service)  
  • MPNS (Microsoft Push Notification Service) 

and many more with an ability to filter push notifications by a customer, location, or any custom-defined tag to quickly send millions of notifications to iOS, Android, Windows, or Kindle devices. 

With the Notification Hubs tagging functionality, you can send push alerts to everyone at once or narrow it to specific customer devices. Tags allow you to segment users by activity, interest, geography, or preference, ensuring that the right content gets delivered to the correct person at the right time. A tag could be up to 120 characters long string that includes alphanumeric and non-alphanumeric characters. 

Azure notification hub diagram

The notification tagged with Data_Scientist only reaches the device registered with the tag Data_Scientist, as shown in the diagram. 

While sending a push notification with Notification Hubs, tag expressions allow you to target specified groups of devices or, more precisely, registrants. We can target individual device registrations only by associating tags with specific device registrations and then targeting those tags. 

Both commercial and consumer scenarios benefit from Notification Hubs. Here are a few things you can do: 

  • Send minimal latency breaking news alerts to millions of people. 
  • Send coupons to user segments who are interested based on their location. 
  • For media, sports, social, gaming, or e-commerce apps, send event or deals-related notifications to users or broadcast to groups. 
  • To attract and advertise to customers, push promotional information to apps. 
  • To notify a user about the status of an activity performed. 

Push notifications are critical for enhancing application engagement and usage in consumer apps and providing the most updated business information in enterprise applications. Because it is resource-efficient for mobile devices, customizable for notification senders, and available even when relevant applications are not operating, leading it to the optimal app-to-user communication service. 

Below in this article, we will cover:  

  • Configuring the Notification Hub for APNS 
  • Prerequisite for Registering / Viewing / Deleting the devices with Notification Hub, 
  • Registering the devices, 
  • Viewing the registered devices, 
  • Deleting the registered devices. 

Configuring the Notification Hub for APNS:

Prerequisites 
  • A Notification Hub deployed in the Azure portal. In case you need to create a Notification Hub refer to this Documentation 
  • In case of Certificate-based authentication, you must have a .p12 certificate file available with the password. 
  • In the case of Token-based authentication, you will require Team ID, Bundle ID, Key ID, and a Token value from a .p8 certificate file. 
  • You can generate the above prerequisite details for authentication modes by following this Documentation. 

To configure the Notification Hub with Apple Push Notification Services, we have two Authentication modes available as below: 

  • Certificate-based 
  • Token-based 

You can select either Certificate or Token,  

For Certificate-based mode: 
  • Select the file icon and select the .p12 file from your local system that you want to upload. 
  • Enter a password associated with the certificate file. 
  • Select Sandbox mode while in development, select Production mode to send push notifications to users who purchased your app from the store. 

For Token-based mode: 
  • Enter the values for Key ID, Bundle ID, Team ID, and Token (obtained from a .p8 certificate file). 
  • Like Certificate-based mode, Select Sandbox mode while in development and select Production mode to send push notifications to users who purchased your app from the store. 

token based mode

Prerequisite for Registering / Viewing / Deleting the devices with Notification Hub:

To Register, View, or Delete the devices with the Notification Hub, we have HTTP’s PUT, GET and DELETE REST API methods. The first and foremost requirement to access these APIs is to generate the SAS Token, used as Authorization Header. 

The Authorization Header should consist of Token generated as specified in SAS Authentication and authorization with Microsoft Azure Active Directory Access Control. 

Microsoft provides a guide to generate SAS tokens using multiple programming languages for using Azure Service Bus REST APIs in this Document. 

For the Python approach to generate SAS tokens for using Azure Service Bus REST APIs, we would require a few details as mentioned below: 

  • Namespace Name: The Namespace resource name associated with Notification Hub (namespace could contain one or multiple hubs). 
  • Notification Hub Name: The Notification Hub resource name. 
  • SAS Name: The Shared Access Key Name. 
  • SAS Value: The Shared Access Key Value. 

You can find the Namespace name and Notification Hub name from the portal when you navigate to Notification Hub in the overview section, as shown below: 

notification hub overview section

You can find those details for the SAS name and value parameters when navigating to Access policies under the Manage section in the left panel. 

You can find two policies with different permissions. We require the details from the “DefaultFullSharedAccessSignature” policy since it has Full permissions (Listen, Manage, Send).  

notification hub access policies

We require copying the connection string from DefaultFullSharedAccessSignature policy, which looks like something shown below and contains the Shared Access Key name and its value. 

Endpoint=sb://test-notification-hub-namespace.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=Zabc123K6Acab42Gb7Ag/ShrTmABCd2svETESTRt456c= 

These details would be required to generate the SAS Token as Authorization Header for accessing APIs. 

Registering the devices with Notification Hub:

Using a Registration or Installation approach, we can accomplish the registration of devices with a Notification Hub. 

An installation/registration will associate the PNS handle with tags that we maintain to send a notification to the appropriate user rather than broadcasting them. In the case of APNS, the PNS handle is a unique device token for each device, and to segregate device handles; we can then use Tags. 

Note: Notification Hubs allow a maximum of 60 tags for each device. 

Compared to the Registration approach, An Installation approach is a much-enhanced approach to registering a device that provides multiple push-related properties. It is the recently developed and enhanced approach to registering your devices with Notification Hub. 

Microsoft provides REST APIs for registration and installation, which are user-friendly and require minimal details. Just a Device Token for APNS is enough to register the device with Notification Hub. At the same time, the Tags are an optional element. 

Below are a few critical points discussed to choose Installation over Registration for registering the device with Notification Hub: 

  • We could use installation REST APIs to repeatedly make that same call while producing the same result, without any second thought of creating Duplicate registration. It would overwrite the existing registration while reflecting the latest change if made any. 
  • The Installation approach provides an exceptional Tag format $InstallationId:{<INSTALLATION_ID_GOES_HERE>}, which enables the Notification Hub to classify the unique devices and send the notifications to particular devices rather than broadcasting them. 

Suppose a user provides Installation id as testinstallation123 for a particular device while registering with Notification Hub. He could then direct the message to the specific device only by applying $InstallationId:{testinstallation123} Tag, without even doing any extra programming stuff to do so. 

This article would move forward with the Installation Approach and a popular tool like Postman to make API calls to register, view, and delete the devices with Notification Hub. 

Microsoft provides multiple REST API Methods to perform these tasks, and we would be using Installation REST API for registering the devices with our Notification Hub: 

Prerequisites:  
  • Namespace Name 
  • Notification Hub Name 
  • SAS Token for Authorization Header 
  • PNS Handle from iOS device 

As discussed earlier in Prerequisite for Registering / Viewing / Deleting the devices with Notification Hub section, we will require the SAS Token generated for Authorization Header, which will look similar to as shown below: 

notification hub installation

And the request body would contain parameters and their value as below: 

  • installationid: Any uniquely identifiable string value. 
  • tags(optional): Any string value that can uniquely identify or group the devices (#,:, _ and – are only accepted characters).  
  • platform: APNS. 
  • pushchannel: The PNS handle or device token from the device (device token must be a hexadecimal digit). 

The Request Body would look something similar to this: 

notification hub request body

Once we send the request, the generated response with status code is 200 with an empty body, which means we have successfully created or overwritten and installation. 

notification hub generated response

Viewing the registered devices with Notification Hub:

There are multiple possible ways to retrieve the registrations, and you can retrieve all registration with a Notification Hub or even retrieve selected registrations using the installationid. 

MSFT provides multiple REST APIs to deal according to the scenario if we have installationid to retrieve particular registration using Read installation or even retrieve all registrations made with the Notification Hub using Read registrations. 

We would retrieve the earlier created registration where we used installationid as 21-2121. To verify if the update gets reflected, we will update its tags and then re-retrieve the registration. 

We would make a GET call by providing only the installationid. The only prerequisites here would be the installationid and the SAS Token for Authorization: 

notification hub prerequisites

We would now add a new tag to the same registration and see if it reflects the update. To add the tag, we would be using the earlier API that we used to register the device, as it will then overwrite an installation. 

notification hub - adding tag

To verify the latest change reflects, let’s now retrieve the installation. 

notification hub - retrieve installation

We were able to retrieve the registered device using the installationid with the latest reflected change. 

We would use the similar API below that would return all registered devices with Notification Hub: 

notification hub - registered hub

We can even see that the installation has generated an exceptional Tag format, which we discussed earlier $InstallationId:{21-2121}, which would help Notification Hub to classify the unique devices and send the notifications to particular devices rather than broadcasting them. 

Deleting a registered device:

To delete multiple registrations in a single go, we currently don’t have any REST API. Possibly, there are two ways to delete a registered device using either the installationid or registration id. 

We would demonstrate by attempting to delete a registered device using the installationid as 21-2121, using the DELETE Method as discussed in this Documentation: 

notification hub-deleting registered device

The installation got successfully removed, and We could see the DELETE Method returned the 204 status code. Even if the installation was not present / was not found, the service will return the status code as 204. 

Later, when we try to retrieve the same registered device after it gets deleted, the GET method used to retrieve devices would return the status code as 404, stating it did not retrieve the installation as below: 

notification hub - retrieve same registered device

Now that we have completed this demonstration, you can configure your Notification Hub to send alerts and notify your customers about the deals and offers via a quick, simple notification sent using Azure.  I hope these steps help you. 

In one of our projects for a cosmetic company, we used Notification Hubs to notify the mobile device users whenever an image successfully uploads to Azure Data Lake Storage (ADLS) containers. Based on specific parameters set to validate the face in the image, a Machine Learning model would accept/reject the uploaded image and notify the device using Azure Notification Hubs.