Sending push notifications to your mobile application can require a lot of development and maintenance. Fortunately in all the Azure products, one will help you to send push notifications cross-platform with minimum efforts: Azure Notification Hubs.
Today we will focus on creating an Azure Resource Manager template to easily setup and deploy a Notification Hub to a resource group. Our ARM template will be created in a new Azure Resource Group deployment project in Visual Studio.
Creation
Let's declare the parameters of the ARM template:
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "notificationHubNamespaceName": { "type": "string", "defaultValue": "[concat('myNotifHubNs', uniqueString(resourceGroup().id))]" }, "notificationHubName": { "type": "string", "defaultValue": "[concat('myNotifHub', uniqueString(resourceGroup().id))]" }, "notificationHubSkuName": { "type": "string", "defaultValue": "free", "allowedValues": [ "free", "basic", "standard" ] } } ... }
Now we will declare the resources composed by the Notification Hub namespace and the Notification Hub:
{ ... "resources": [ { "apiVersion": "2014-09-01", "name": "[parameters('notificationHubNamespaceName')]", "type": "Microsoft.NotificationHubs/namespaces", "location": "[resourceGroup().location]", "sku": { "name": "[parameters('notificationHubSkuName')]" }, "properties": { "name": "[parameters('notificationHubNamespaceName')]", "namespaceType": "NotificationHub" }, "resources": [ { "apiVersion": "2014-09-01", "name": "[concat(parameters('notificationHubNamespaceName'),'/',parameters('notificationHubName'))]", "type": "Microsoft.NotificationHubs/namespaces/notificationHubs", "location": "[resourceGroup().location]", "dependsOn": [ "[concat('Microsoft.NotificationHubs/namespaces/', parameters('notificationHubNamespaceName'))]" ], "properties": { "name": "[parameters('notificationHubName')]" }, "tags": { "displayName": "NotificationHub" } } ], "tags": { "displayName": "NotificationHubNamespace" } } ] ... }
We can pay attention to several things here:
And to finish, we will output the primary connection string of the hub:
{ ... "outputs": { "NotificationHubNamespaceConnectionString": { "type": "string", "value": "[listKeys(resourceId('Microsoft.NotificationHubs/namespaces/notificationHubs/authorizationRules', parameters('notificationHubNamespaceName'), parameters('notificationHubName'), 'DefaultFullSharedAccessSignature'), providers('Microsoft.NotificationHubs', 'namespaces/notificationHubs').apiVersions[0]).primaryConnectionString]" } } }
As you can notice, we take advantage of the ARM template function listKeys and the ARM template function providers.
The function providers is useful to get the latest API version for a specific namespace, whereas listkeys is the function that will allow us to get the properties for a specific key name.
By default when a new hub is created, two access keys are created: DefaultListenSharedAccessSignature and DefaultFullSharedAccessSignature. In our template we retrieve the primary connection string for the second one.
Example of use
The ARM template is now ready, let's open a Windows PowerShell and try it:
.\Deploy-AzureResourceGroup.ps1 -ResourceGroupName 'MyResourceGroupName' -ResourceGroupLocation 'canadaeast' -TemplateFile '.\azuredeploy.json'
...
OutputsString :
Name Type Value
=============== ========================= ==========
notificationHubNamespaceConnectionString String Endpoint=sb://mynotifhubnsjibxupcseloca.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=b0zOZuqc/mvrcLq8M49mfLomHBw4PH56uazMM/8r22Q=
If everything goes well, you should see the same kind of output as above.
To go further
In the template we are outputting the connection string as an example. But in a more advance scenario with a Website you could directly set the application setting that require the connection string like the following:
{ ... "resources": [ { ... "resources": [ { "apiVersion": "2015-08-01", "name": "appsettings", "type": "config", "dependsOn": [ "[resourceId('Microsoft.NotificationHubs/namespaces/notificationHubs', parameters('notificationHubNamespaceName'), parameters('notificationHubName'))]", "[resourceId('Microsoft.Web/sites', parameters('myWebsiteName'))]" ], "properties": { "NotificationHubConnectionString": "[listKeys(resourceId('Microsoft.NotificationHubs/namespaces/notificationHubs/authorizationRules', parameters('notificationHubNamespaceName'), parameters('notificationHubName'), 'DefaultFullSharedAccessSignature'), providers('Microsoft.NotificationHubs', 'namespaces/notificationHubs').apiVersions[0]).primaryConnectionString]", "NotificationHubPath": "[parameters('notificationHubName')]" } } ] ... } ] ... }
Summary
We have seen how to create an ARM template that will deploy an Azure Notification Hub and output the primary connection string.
You can download the example solution here:
Or
Browse the GitHub repository
Please feel free to comment or contact me if you have any question about this article.