Why Notification Hub installations are not removed on expiration time?

Oleksandr Pastukhov 20 Reputation points
2024-07-03T15:44:49.25+00:00

Hello,

we're working on an application that uses Azure Notification Hub for sending push notifications. The application utilizes Notification Hub .NET SDK v4.2.0 to communicate with the Notification Hub. To register a device for push notifications, the app creates an installation with an expiration time specified and persists it in the Notification Hub using the _hubClient.CreateOrUpdateInstallationAsync(installation, token) method of the SDK. However, per our observation, such installations are not removed after they pass the expiration date. For example, in the screenshot below you can see that there is an installation present in the Notification Hub that has an expiration date set to 07/01/2024, so it has not been removed considering that two days have passed since the expiration time.

Is it a bug in the Notification Hub or are we missing something?

If any more details are needed, please let me know.

User's image

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
334 questions
0 comments No comments
{count} votes

Accepted answer
  1. brtrach-MSFT 16,926 Reputation points Microsoft Employee
    2024-12-19T06:06:24.4333333+00:00

    To manage expired installations, you might need to implement a cleanup process in your application. This process could periodically check for and remove installations that have passed their expiration date. You can use the GetAllRegistrationsAsync method to retrieve all installations and then filter out the expired ones for deletion.

    Here's a sample code snippet to illustrate this process:

    using Microsoft.Azure.NotificationHubs;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    public class NotificationHubCleanup
    {
        private NotificationHubClient _hubClient;
    
        public NotificationHubCleanup(string connectionString, string hubName)
        {
            _hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
        }
    
        public async Task CleanupExpiredInstallationsAsync()
        {
            var allInstallations = await _hubClient.GetAllRegistrationsAsync(100); // Adjust the number as needed
            var expiredInstallations = new List<string>();
    
            foreach (var installation in allInstallations)
            {
                if (installation.ExpirationTime < DateTime.UtcNow)
                {
                    expiredInstallations.Add(installation.InstallationId);
                }
            }
    
            foreach (var installationId in expiredInstallations)
            {
                await _hubClient.DeleteInstallationAsync(installationId);
            }
        }
    }
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.