7
votes

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

3

3 Answers

26
votes

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.

7
votes

There is one more way if some one just want to retrieve the details of registered devices just for a knowledge not for application purpose. Service Bus Explorer is available. You can download the project and extract it and run using visual studio.

You will be able to connect to azure services by providing the connection string and owner key. I used this to see the registered devices and sending the test notifications etc. Its a great helpful tool.

Hope this can help some one.

0
votes

short Code:

    private async Task<List<RegistrationDescription>> GetAllRegisteredDevicesAsync()
    {
        List<RegistrationDescription> allRegistrations = new List<RegistrationDescription>();

        var hub = NotificationHubClient.CreateClientFromConnectionString(
            Settings.Default.AzureNotificationsMobileAppFullSharedAccessListenerConnection,
            Settings.Default.AzureNotificationsMobileAppHubName,
            Settings.Default.AzureNotificationsTestSendMode);

        CollectionQueryResult<RegistrationDescription> page = null;
        do
        {
            page = await hub.GetAllRegistrationsAsync(page?.ContinuationToken, 0);
            allRegistrations.AddRange(page);
        }
        while (!string.IsNullOrWhiteSpace(page.ContinuationToken));

        return allRegistrations;
    }