How to update Expiration Time in Azure Notification Hub registration?

前端 未结 2 1776
感情败类
感情败类 2021-01-21 08:22

I\'ve been working with Azure Notification Hubs for awhile. However, I created a new Notification Hub for a new project and I\'ve noticed some very odd behavior. Whenever I cr

相关标签:
2条回答
  • 2021-01-21 08:38

    You can do that, but on hub level, not on registration level. Check out Improved Per Message Telemetry and device expiry for Notification Hubs blog post:

    To take advantage of this expiry change, simply update your notification hub’s Time To Live property. This can be done through REST or our .NET SDK:

    var namespaceManager = NamespaceManager.CreateFromConnectionString("connectionstring");
    NotificationHubDescription hub = namespaceManager.GetNotificationHub("foo");
    hub.RegistrationTtl = TimeSpan.MaxValue;
    namespaceManager.UpdateNotificationHub(hub);
    

    To do that via the REST API, check out Update Notification Hub method, which takes NotificationHubDescription body, which has a RegistrationTtl node in it. That should be a REST equivalent of the SDK code snippet above.

    0 讨论(0)
  • 2021-01-21 08:47

    The documentation is outdated, I had to open a ticket with Microsoft to be able to do this in 2020.

    I created a console app and added the following nuget packages -

    https://www.nuget.org/packages/Microsoft.Azure.Management.NotificationHubs https://www.nuget.org/packages/Microsoft.Azure.Management.ResourceManager.Fluent/

    Install-Package Microsoft.Azure.Management.NotificationHubs -Version 2.3.2-preview

    Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.34.0

    Then I wrote this method -

        private async Task SetNotificationHubRegistrationTimeToLive()
        {
            // Login to Azure using az login
            // az account set -s <name or ID of subscription> to set the proper subscription
            // Get credentials: "az ad sp create-for-rbac --sdk-auth"
            // See https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest and https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart
    
            var clientId = "ec1b...";
            var clientSecret = "oJJ6...";
            var tenantId = "2b86...";
    
            var credentials =
                    SdkContext
                    .AzureCredentialsFactory
                    .FromServicePrincipal(
                        clientId,
                        clientSecret,
                        tenantId,
                        AzureEnvironment.AzureGlobalCloud);
    
            var client = new NotificationHubsManagementClient(credentials)
            {
                SubscriptionId = "yoursubscriptionid"
            };
    
            var resourceGroupName = "yourgroup";
            var namespaceName = "yournamespace"; // this should NOT be the namespace full name beam-dev-notification-hub-namespace-free.servicebus.windows.net
            var notificationHubName = "yournotificationhub";
    
            var timeSpan = new TimeSpan(days: 90, hours: 0, minutes: 0, seconds: 0);
            var registrationTtlTimeSpanString = timeSpan.ToString();
    
            var notificationHub = await client.NotificationHubs.GetAsync(resourceGroupName, namespaceName, notificationHubName);
    
            await client
                  .NotificationHubs
                  .CreateOrUpdateAsync(
                    resourceGroupName,
                    namespaceName,
                    notificationHubName,
                    new NotificationHubCreateOrUpdateParameters(notificationHub.Location)
                    {
                        RegistrationTtl = registrationTtlTimeSpanString
                    });
        }
    

    You will then see in https://portal.azure.com/ in your notification hub properties -

    0 讨论(0)
提交回复
热议问题