使用.NET中的Azure Notification Hub向所有已注册的设备发送推送通知

时间:2016-09-20 09:13:26

标签: azure push-notification notifications azure-notificationhub

我正在使用Azure Notification Hub,我想向.NET后端中的所有已注册设备发送推送通知消息。但我不确定这种方式是否会发送到所有设备,因为我无法检查接收推送消息的设备数量。 那么,如何,我可以向所有设备发送推送消息,或者可以确保这种方式是正确的?

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 个答案:

答案 0 :(得分:0)

您需要使用Routing and Tag Expressions中描述的标记:

  

定位特定注册的唯一方法是关联它们   使用标记,然后定位该标记。如注册中所述   管理,以便接收应用程序必须的推送通知   在通知中心上注册设备句柄。注册后   在通知集线器上创建,应用程序后端可以发送推送   通知给它。应用程序后端可以选择   注册以下面的特定通知为目标   方式:

     
      
  1. 广播:通知中心内的所有注册都会收到   通知。

  2.   
  3. 标记:包含指定标记的所有注册都会收到   通知。

  4.   
  5. 标记表达式:所有与其匹配的注册   指定的表达式接收通知。

  6.   

注意,您需要考虑there're limitations on broadcast messages

请查看Breaking News App Sample有关如何使用广播通知的详细信息。

答案 1 :(得分:0)

如果您未指定任何标记表达式,则表示它是广播。所有设备都将收到通知。您可以使用“每封邮件遥测”跟踪接收的设备数量。请参阅以下链接。

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

相关问题