一个通知中心可以处理多种类型的通知吗?

Can a single notification hub handle multiple types of notifications?

如何设置推送通知以便用户可以select订阅哪个通知?以天气应用为例,用户 A 想订阅龙卷风来临时的通知,但用户 B 不关心这个,只想在洪水来临时收到通知。所以在应用程序中有一个设置允许用户订阅这些通知中的一个或两个。

现在在我的后端我有一个网络作业,它从在线资源收集天气数据并存储在 SQL 数据库中,我在 Azure 中创建了一个通知中心,并像这个例子一样将代码添加到我的网络作业

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification#optional-send-notifications-from-a-console-app

 private static async void SendNotificationAsync()
 {
     NotificationHubClient hub = NotificationHubClient
         .CreateClientFromConnectionString("<connection string with full access>", "<hub name>");
     var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";
     await hub.SendWindowsNativeNotificationAsync(toast);
 }

当我收到某些指示洪水或龙卷风的天气数据时,我可以做这样的事情来发送通知

if(flood)
SendNotificationAsync(flood) //only sends notification to User B
else if(tornado)
SendNotificationAsync(tornado) //only send notification to User A

这可以使用单个通知中心来完成吗?还是我必须创建多个集线器?

爱罗塔夫拉斯,

要做到这一点,您必须查看标签。在您的情况下,您将有两个标签:"Flood""Tornado".

如果您检查 SendNotificationAsync,您会注意到您可以提供标签

public Task<NotificationOutcome> SendNotificationAsync(Notification notification, IEnumerable<string> tags)

我不知道您是使用安装模式还是注册模式来注册您的设备,但是 InstallationRegistrationDescription 都有一个名为 Tags 的 属性。

当 registering/updating 用户的设备时,根据用户想要订阅的内容,您将设置适当的标签。

示例:

龙卷风注册

        var installation = new Installation()
        {
            InstallationId = installationId ,
            Platform = platform ,
            PushChannel = token,
            Tags = new string[] { "Tornado" }
        };

        await notificationHubClient.CreateOrUpdateInstallationAsync(installation);

向 Tornado 订阅者发送通知

   await notificationHubClient.SendNotificationAsync(notification, new string[] { "Tornado" });