【问题标题】:Can a single notification hub handle multiple types of notifications?单个通知中心能否处理多种类型的通知?
【发布时间】:2017-08-02 05:39:57
【问题描述】:

如何设置我的推送通知,以便用户可以选择订阅哪个通知?以天气应用为例,用户 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

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

【问题讨论】:

    标签: azure push-notification uwp azure-notificationhub


    【解决方案1】:

    埃罗塔夫拉斯,

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

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

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

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

    在注册/更新用户的设备时,根据用户想要订阅的内容,您将设置适当的标签。

    示例:

    龙卷风注册

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

    【讨论】:

    • 我实际上是在尝试通过 WNS 使用通道 uri 注册设备。这是大多数示例展示如何做到这一点的方式。这就是为什么我从未在示例中看到任何关于标签的内容。我不知道现在该做什么,但我会尝试找到更多关于你提到的注册模型的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多