Search code examples
multithreadingazureazure-notificationhub

Azure Notification Hub Sending thousands of notifications and getting back results from each


We're setting up Push Notifications for our app. We are creating a console app now that will determine which users to send to, and then send them to said users. What's not obvious to us at this point is how do we know when each one of them completed? Or didn't complete? There's not a lot of documentation provided by Microsoft (big surprise there) and whatever documentation there is doesn't really explain how to read the responses.

For instance, here's an example snippet of what we think we need to implement, because we could have thousands of people receiving a notification at one time, we would want them to run parallel and not block UI.

public async Task GenerateNotifications()
{
NotificationHubClient hub =           NotificationHubClient.CreateClientFromConnectionString(AppHelper.AzureNotificationHubConnectionString, "myhub");

List<Task<NotificationOutcome>> notificaitonTasks = new List<Task<NotificationOutcome>>();

for (int i = 0; i < 10; i++)
{
    var notification = new
    {
        aps = new
        {
            alert = string.Format("Awesome Notification {0}", i),
            sound = "default"
        }
    };

    string notificationJSON = JsonConvert.SerializeObject(notification);
    notificaitonTasks.Add(hub.SendAppleNativeNotificationAsync(notificationJSON, "mytag"));
}

await Task.WhenAll(notificaitonTasks);

}

This makes sense to us, we use the WhenAll method to execute all the tasks on parralel threads. But then is there a way to know what happens with EACH task that gets run? For instance, the ContinueWith method seems to do what we want, except we think that this will only run after ALL the tasks are completed, and not after each one (please correct me if I'm wrong).

So, is there a way to read each response of a WhenAll call? If no, is there a better way to do what we are trying to do? I will supply any other information needed, please just ask.


Solution

  • You can use per message Telemetry feature to get the result of each notification sent. You will need to however upgrade to Standard Tier to do that. See link Per Message Telemetry Per Message Telemetry Blog

    On a side note you can use NotificationOutcome.Result property by using the EnableTestSend property as shown in the link EnableTestSend property(Search for Debug Failed notifications). This will only send the notification to 10 devices that matches your condition. This is primarily used for debugging purpose