List of registered Azure Notification Hub devices

I am following This Post to work with the azure notification center. What I'm trying to do is create a web api that registers devices using the Azure notification hub. When I submit a device registration request, as shown in the article, it enters the azure notification hub.

Below is a screenshot of my azure portal. Which indicates that there was a request for registration.

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

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

Now I have a few questions:

1) How can I find out the registered device data?

2) How can I send a test notification to ios devices from the back. Below is the code that I use to send test notifications.

  var payload = string.Format(toastTemplate, message); hub.SendAppleNativeNotificationAsync(payload, "worldnews"); 

3) If I use the web api as the back end, do I need to configure the details of the ios app in the notification house? Upload the certificate and other data to the azure portal?

enter image description here

+7
ios asp.net-web-api azure azure-notificationhub
source share
3 answers

Your first problem is how you call GetAllRegistrationsAsync . The parameter is not the maximum number of registrations that you want to return. This is the index of the first registration you want. In most scenarios this will 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) 

Keep in mind that this method returns a maximum of 100 registrations. If you want more, you will need to use the ContinuationToken.

Here is the code I use to register:

 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; } 

Please note that this method should only be used if you have only a few hundred, possibly several thousand registrations. If you have dozens, hundreds of thousands or millions of registrations, you should not use this method and find a more effective method of finding what you need.

+17
source share

There is another way if someone just wants to get information about registered devices only for obtaining knowledge not for the purpose of application. Service bus conductor . You can download the project and extract it and run it using visual studio.

You can connect to azure services by providing a connection string and an owner key. I used this to view registered devices and send test notifications, etc. This is a great tool.

Hope this helps someone.

+5
source share

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; } 
0
source share

All Articles