Azure Notification Hub Registered Device list

Sachin Trivedi picture Sachin Trivedi · Jun 27, 2014 · Viewed 7.2k times · Source

I am following This Post to work with azure notification hub. What i am trying to do is creating the web api that registers the devices with the Azure notification hub. When i send the request for registering the device as shown in the article it hits the azure notification hub.

Below is the screen shot of my azure portal. Which shows there was a request for registration.

But when i try to get the details of the registered devices using the following code it is always 0.

var registrationsCount = await hub.GetAllRegistrationsAsync(Int32.MaxValue);
return registrationsCount.Count().ToString();

Now i have few questions :

1 ) how can i explore the registered device details ?

2 ) How i can i send a test notification to the ios devices from back end. Below is the code that i am using to send test notifications.

 var payload = string.Format(toastTemplate, message);

 hub.SendAppleNativeNotificationAsync(payload, "worldnews");

3 ) If i am using the web api as back end is it necessary to configure the ios app details in azure notification hub ? i.e uploading the certificate and other details on azure portal ?

enter image description here

Answer

Alan McBee picture Alan McBee · Jul 3, 2014

Your first problem is how you are calling GetAllRegistrationsAsync. The parameter is not the maximum count of registrations you want back. It's the index of the first registration you want. Under most scenarios, that would be 0, not Int32.MaxValue

See: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.notificationhubs.notificationhubclient#Microsoft_Azure_NotificationHubs_NotificationHubClient_GetAllRegistrationsAsync_System_Int32_

public Task<CollectionQueryResult<RegistrationDescription>> 
    GetAllRegistrationsAsync(int top)

Bear in mind, also, that this method returns a maximum of 100 registrations. If you want more, you'll need to use the ContinuationToken.

Here's the code I use to get the registrations:

internal async Task<List<RegistrationDescription>> GetAllRegisteredDevicesAsync()
{
    var hub = NotificationHubClient.CreateClientFromConnectionString(
        Settings.Default.AzureNotificationsMobileAppFullSharedAccessListenerConnection,
        Settings.Default.AzureNotificationsMobileAppHubName,
        Settings.Default.AzureNotificationsTestSendMode);

    var allRegistrations = await hub.GetAllRegistrationsAsync(0);
    var continuationToken = allRegistrations.ContinuationToken;
    var registrationDescriptionsList = new List<RegistrationDescription>(allRegistrations);
    while (!string.IsNullOrWhiteSpace(continuationToken))
    {
        var otherRegistrations = await hub.GetAllRegistrationsAsync(continuationToken, 0);
        registrationDescriptionsList.AddRange(otherRegistrations);
        continuationToken = otherRegistrations.ContinuationToken;
    }

    return registrationDescriptionsList;
}

Note that this method should only be used if you have only a few hundred, perhaps a few thousand registrations. If you have tens, hundreds of thousands or millions of registrations, you should not use this method, and find a more efficient method to find what you need.