Menu Home

Provision Azure Notification Hub with Azure Resource Manager Templates

This weeks ARM challenge was to provision an Azure Notification hub, with client credentials for Google Cloud Messaging (GCM) as part of a larger ARM template. Unfortunately how to do so is not documented, but we are not completely out of luck because the schema for the notification hub is available in the azure-resoruce-manager-schemas git hub repository.

Having the schema definition is quite a big help, and it lets us author the template without much trouble (at least for the GCM notification type).

Here is the template I ended up using

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "Gcm.GoogleApiKey": {
      "type": "string",
      "metadata": {
        "description": "Google Cloud Messaging API Key"
      },
      "defaultValue": "AIzaSyAyp9MernKgMS3wFNM3yNWByiP-TaGrqEg"
    },
  },
  "variables": {
    "hubVersion": "[providers('Microsoft.NotificationHubs', 'namespaces').apiVersions[0]]",
    "notificationHubNamespace": "[concat('hubv2', uniqueString(resourceGroup().id))]",
    "notificationHubName": "notificationhub"
  },
  "resources": [
    {
      "name": "[variables('NotificationHubNamespace')]",
      "location": "[resourceGroup().location]",
      "type": "Microsoft.NotificationHubs/namespaces",
      "apiVersion": "[variables('hubVersion')]",
      "comments": "Notification hub namespace",
      "properties": {
        "namespaceType": "NotificationHub"
      },
      "resources": [
        {
          "name": "[concat(variables('NotificationHubNamespace'),'/',variables('NotificationHubName'))]",
          "location": "[resourceGroup().location]",
          "type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
          "apiVersion": "[variables('hubVersion')]",
          "properties": {
            "GcmCredential": {
              "properties": {
                "googleApiKey": "[parameters('Gcm.GoogleApiKey')]",
                "gcmEndpoint": "https://android.googleapis.com/gcm/send"
              }
            }                    
          },
          "dependsOn": [
            "[concat('Microsoft.NotificationHubs/namespaces/', variables('NotificationHubNamespace'))]"
          ]
        }
      ]
    }
  ],
  "outputs": {
  }
}

There is not much to this basic template, all you need to know it to use the resource type Microsoft.NotificationHubs/namespaces, to create a notification hub namespace. And then the nested resource should look like, to create the actual hub.

 {
          "name": "[concat(variables('NotificationHubNamespace'),'/',variables('NotificationHubName'))]",
          "location": "[resourceGroup().location]",
          "type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
          "apiVersion": "[variables('hubVersion')]",
          "properties": {
            "GcmCredential": {
              "properties": {
                "googleApiKey": "[parameters('Gcm.GoogleApiKey')]",
                "gcmEndpoint": "https://android.googleapis.com/gcm/send"
              }
            }                    
          },
          "dependsOn": [
            "[concat('Microsoft.NotificationHubs/namespaces/', variables('NotificationHubNamespace'))]"
          ]
        }

.

For GCM credentials, the properties should contain

"GcmCredential": {
              "properties": {
                "googleApiKey": "[parameters('Gcm.GoogleApiKey')]",
                "gcmEndpoint": "https://android.googleapis.com/gcm/send"
              }
            }  

Note that the googleApiKey should be a correct one as the ARM resource provider actually do some input validation.

If you want to try to use some of the other messaging systems that notification hub supports, you can find the properties in the aforementioned schema. I tried getting the apple push notification service configured as well, but it requires a real certificate from apple that I don’t currently have. Once I have one, I will post a follow up.

"apnsCredential": {
              "properties": {
                "apnsCertificate": "[parameters('APNS.Certificate')]",
                "certificateKey": "[parameters('APNS.certificateKey')]",
                "endpoint": " gateway.sandbox.push.apple.com or gateway.push.apple.com",
              }
            }

You can grab my entire project from github https://github.com/sjkp/ARM.NotificationHub

Categories: Software Windows Azure

Tagged as:

Simon J.K. Pedersen

6 replies

  1. how to include the APNS.Certificate in the arm template, it is a file path should we need to provide. I am getting error ‘failed with message ‘Bad Request’

  2. Last I tried I was unable to get the Apple certificates to deploy, since I didnt have a valid certificate. I might try again in the upcoming weeks will post an update if I get it to work.

  3. APNS.Certificate holds contents of certificate file in PCKS#12 format (with private key included) encoded in Base64. So if you’re trying to use PowerShell to deploy your environment you could use something like that to encode your cert to Base64 (tested with actual Apple cert):

    $certificateContentBytes = [System.IO.File]::ReadAllBytes($certificateFile)
    $certificateContentBase64 = [Convert]::ToBase64String($certificateContentBytes)

  4. For Apple push notification service:

    “apnsCredential”: {
    “properties”: {
    “apnsCertificate”: “[parameters(‘APNS.Certificate’)]”,
    “certificateKey”: “[parameters(‘APNS.certificateKey’)]”,
    “endpoint”: “gateway.push.apple.com”
    }
    }

    APNS.Certificate:
    This is the Apple Push Notification certificate in base 64 string-format.
    You can use PowerShell to convert the certificate like this:

    $fileContentBytes = get-content ‘MyPushCert.p12’ -Encoding Byte

    [System.Convert]::ToBase64String($fileContentBytes) | Out-File ‘MyPushCert.txt’

    APNS.certificateKey:
    This is the password you specified when you exported the certificate.

    For Windows push notification service:

    “wnsCredential”: {
    “properties”: {
    “packageSid”: “[parameters(‘WNS.PackageSid’)]”,
    “secretKey”: “[parameters(‘WNS.SecretKey’)]”
    }
    }

    WNS.PackageSid:
    This is the Package SID for the Windows Store app

    WNS.SecretKey:
    This is the Application Secret

    You find these properties in the Application Registration Portal, https://apps.dev.microsoft.com

    (The property “windowsLiveEndpoint” should not be included.)

  5. Thanks for the article. I am trying to use APNS also for configuring the azure notification hub using the ARM template. I tried multiple times with the apns certificate and the apns certificate key but it is not working for me. Can you please help me to provide the complete solution including the details for the APNS. Any help on this is much appreciated.

  6. I don’t have it included in any templates of my own so can’t provide details on how to do it. Doesn’t David’s comment help?

Leave a Reply

Your email address will not be published. Required fields are marked *