Azure推送通知API,用于获取总传出通知和已注册设备的数量

时间:2018-01-04 07:27:41

标签: azure azure-notificationhub azure-sdk-.net

如何检索总传出通知的数量和已注册设备的数量? MS Azure是否提供任何API来执行此操作?我想以JSON或XML检索结果。 谢谢你的重播。 enter image description here

1 个答案:

答案 0 :(得分:0)

  

如何检索总传出通知的数量和已注册设备的数量? MS Azure是否提供任何API来执行此操作?

我们可以使用Azure monitor REST API来做到这一点。

GET https://management.azure.com/{resourceUri}/providers/microsoft.insights/metrics?timespan={timespan}&interval={interval}&metric={metric}&aggregation={aggregation}&$top={$top}&$orderby={$orderby}&$filter={$filter}&resultType={resultType}&api-version=2017-05-01-preview

我们可以从此document获取Azure监视器支持的指标。您的案例中的resourceUri是

var resourceUri = $"subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.NotificationHubs/Namespaces/{notificationamespace}/NotificationHubs/{notificationHubName}";

如果您想从Supported metrics with Azure Monitor获取已注册的设备,我们就可以知道。 metric = registration.all

如果您想获得总发送通知 metric = notificationhub.pushes

我们也可以使用Azure SDK Microsoft.Azure.Management.Monitor.Fluent来做到这一点。以下是演示代码。如何获取tenatId,secretKey或应用程序请参阅Create an Azure Active Directory application

var azureTenantId = "tenantId";
var azureSecretKey = "secret key";
var azureAppId = "Application Id";
var subscriptionId = "subscription Id";
var resourceGroup = "resource group";
var notificationamespace = "notification name space";
var notificationHubName = "notification hub ";
var serviceCreds = ApplicationTokenProvider.LoginSilentAsync(azureTenantId, azureAppId, azureSecretKey).Result;
MonitorClient monitorClient = new MonitorClient(serviceCreds) { SubscriptionId = subscriptionId };
var resourceUri = $"subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.NotificationHubs/Namespaces/{notificationamespace}/NotificationHubs/{notificationHubName}";
var metricNames = "name.value eq 'notificationhub.pushes'"; //if you want to get the registered devices please change to  registration.all
string timeGrain = " and timeGrain eq duration'PT24H'"; 
string startDate = " and startTime eq 2018-01-01T00:00:00.919Z"; 
string endDate = " and endTime eq 2018-01-04T19:00:00.919Z"; 
var odataFilterMetrics = new ODataQuery<MetricInner>(
            $"{metricNames}{timeGrain}{startDate}{endDate}");
var metrics = monitorClient.Metrics.ListWithHttpMessagesAsync(resourceUri, odataFilterMetrics).Result;

测试结果:

enter image description here