1

background:

I am developing a react native app with a .net-core backend and I am using Azure notification hub to send push notifications. I don't want useless Id's piling up in the notification Hub so I would like to set a time to live on the notification hub, and everytime a user logs in I will update the hub with createOrUpdateInstallationAsync. When I set the time to live to one day I can see that the expiration time on the registrations are updating every time I log into the app, however if I set the time to live longer than this, the expiration dates stop updating.

Question:

Why does the expiration date not get updated for longer time to live, and What is the recommended way to achieve what I am trying to achieve? When I try to find answers to this question all I can see is guides on how to update the time to live.

J.Kennedy
  • 41
  • 4

1 Answers1

0

Here is one workaround to update expiration date As suggested by @ehuna , "Make sure that you have installed the following nuget packages in your console app

  • Install-Package Microsoft.Azure.Management.NotificationHubs -Version 2.3.2-preview
  • Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.38.0

Wrote the below 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://learn.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest and https://learn.microsoft.com/en-us/azure/cloud-shell/quickstart

        var clientId = "your client id";
        var clientSecret = "your client secret";
        var tenantId = "your tenant id";

        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 that Registration time to live is 90 days."

For more information please refer the below links:

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • Thanks for the answer. I can change the time to live no problem. My question is why does the expiration date not update when calling CreateOrUpdateInstallationAsync. I have set the time to live to 90 days. But now when I update the installation, sometimes the expiration date updates and sometimes it does not. I'm wondering why it fails to update expiration date when the CreateOrUpdateInstallationAsync executes without any errors. – J.Kennedy Dec 10 '21 at 20:55
  • Thanks @J.Kennedy, Could you please refer this MS DOC :https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-registration-management#example-code-to-register-with-a-notification-hub-from-a-backend-using-an-installation – AjayKumarGhose Dec 12 '21 at 05:17
  • I have read the docs but this doesn’t answer my question. If it does and I just missed it, can you please provide more information about where my question is answered in the docs? – J.Kennedy Dec 13 '21 at 11:01