Share via

Programmatic Access

You can access notification hubs telemetry data programmatically, analogous to Microsoft Azure Service Bus metrics (using the REST identifiers provided in the preceding tables to access respective metrics).

Step 1: Create a certificate

First, create a certificate to access your Azure subscription resources. In Windows, do the following:

  1. Open Visual Studio administrator command prompt, and type the following command:

    makecert -sky exchange -r -n "CN=<CertificateName>" -pe -a sha1 -len 2048 -ss My "<CertificateName>.cer"
  2. Run Certmgr.msc, click Personal on the left-hand side, then right-click the certificate you created and click All Tasks, then Export.


  3. Follow the wizard and choose the option to not export the private key. Choose the option to export a CER cert, and then provide a filename ending with .cer.


  4. Repeat the export process, this time choosing to export the private key in a PFX file. Then select a name ending with .PFX.

Step 2: Upload the certificate to Azure

Now upload your .CER file to enable your certificate to perform operations on your Azure resources.

  1. In the Azure management portal, click Settings on the left, and then click Management Certificates.

  2. Click Upload at the bottom of the screen, and then select your .CER file.

  3. Take note of your subscription ID that you want to manage.


    The subscription ID must be the for the subscription that contains the notification hub.


Step 3: Access metrics via a REST interface

In order to read telemetry, you must issue REST calls to a URL constructed according to the rules specified in Microsoft Azure Service Bus metrics (using the metric names reported in the previous section).

The following code is a sample that retrieves the number of successful pushes aggregated in 5-minute intervals since 2013-08-06T21:30:00Z (remember to replace the subscriptionIds, namespace name, notification hub name, and pfx cert path with your values).

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace telemetry1
    class Program
        [DataContract(Name = "properties", Namespace = "")]
        public class MetricValue
            [DataMember(Name = "Timestamp")]
            public DateTime Timestamp { get; set; }

            [DataMember(Name = "Min")]
            public long Min { get; set; }

            [DataMember(Name = "Max")]
            public long Max { get; set; }

            [DataMember(Name = "Total")]
            public long Total { get; set; }

            [DataMember(Name = "Average")]
            public float Average { get; set; }

        static void Main(string[] args)
            string uri = @"{subscriptionId}/services/ServiceBus/namespaces/{namespaceName}/NotificationHubs/{hubName}/metrics/outgoing.allpns.success/rollups/PT5M/Values?$filter=Timestamp%20gt%20datetime'2014-08-06T21:30:00Z'";
            HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(uri);
            sendNotificationRequest.Method = "GET";
            sendNotificationRequest.ContentType = "application/xml";
            sendNotificationRequest.Headers.Add("x-ms-version", "2015-01");
            X509Certificate2 certificate = new X509Certificate2(@"{pathToPfxCert}", "{certPassword}");

                HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();

                using (XmlReader reader = XmlReader.Create(response.GetResponseStream(), new XmlReaderSettings { CloseInput = true }))
                    SyndicationFeed feed = SyndicationFeed.Load<SyndicationFeed>(reader);

                    foreach (SyndicationItem item in feed.Items)
                        XmlSyndicationContent syndicationContent = item.Content as XmlSyndicationContent;
                        MetricValue value = syndicationContent.ReadContent<MetricValue>();
            catch (WebException exception)
                string error = new StreamReader(exception.Response.GetResponseStream()).ReadToEnd();