2
votes

I'm working with Azure Notification Hub, and I want to send a push notification message to all registered device in .NET backend. But I'm not sure this way will send to all devices because I don't have the way to check the number of devices received push message. So, how to, I can send a push message to all devices or can make sure this way is correct?

public static async Task<bool> SendBroadcast(string msg)
    {
        try
        {
            var notificationHubClient = NotificationHubClient.CreateClientFromConnectionString(ConfigurationManager.AppSettings["ServiceBusPushNotificationConnectionString"], ConfigurationManager.AppSettings["ServiceBusPushNotificationName"]);
            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("message", msg);
            param.Add("alert", msg);
            var template = new TemplateNotification(param);
            var result = await notificationHubClient.SendNotificationAsync(template);
            Console.WriteLine(JsonConvert.SerializeObject(result));
            return true;
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return false;
        }
    }
2
Small note, result is of type NotificationOutcome which has prop Failure and prop Success both of type long. So you return true, while it can be false (fail)?JP Hellemons

2 Answers

1
votes

If you don't specify any tag expression, that means it's broadcast. All devices will receive the notification. You can track how many devices are received through using Per Message Telemetry. Please see below links for same.

https://msdn.microsoft.com/en-us/library/azure/mt608135.aspx https://azure.microsoft.com/en-us/blog/push-notification-hub-telemetry-expiry-update/

0
votes

You need to use tags as described in Routing and Tag Expressions:

The only way to target specific registrations is to associate them with a tag, then target that tag. As discussed in Registration Management, in order to receive push notifications an app has to register a device handle on a notification hub. Once a registration is created on a notification hub, the application backend can send push notifications to it. The application backend can choose the registrations to target with a specific notification in the following ways:

  1. Broadcast: all registrations in the notification hub receive the notification.

  2. Tag: all registrations that contain the specified tag receive the notification.

  3. Tag expression: all registrations whose set of tags match the specified expression receive the notification.

Note, there're limitations on broadcast messages that you need to take into account.

Take a look at the Breaking News App Sample on details about how to use broadcast notifications.