azure - Windows 手机 8.1 : Send notification to specified user

标签 azure push-notification windows-phone-8.1

我在使用 Windows Azure 移动服务时遇到问题。我创建了 PushServiceMobileService,在我的移动应用程序中是 notyifyAllUsers 服务调用,一切正常,但是如何制作 Controller ,该 Controller 仅向指定用户发送通知(例如“您有 2 个新 friend ”)?我知道移动应用程序启动时生成的 channel.Uri 是我的请求的目的地,但所有在此 href 上发送 http 请求时,我都有 Http 400 响应。您能告诉我如何构建该请求吗?非常感谢。

诗。对不起我的英语;)

最佳答案

我想向您展示我创建的在我的应用程序中使用的示例。 我还使用 Azure 移动服务和通知中心。

让我将其分为两部分:

  1. Windows Phone 8.1 代码。
  2. 测试用于发送推送通知代码的应用程序。

    在 App.xaml.cs(Windows Phone 项目)类中,我创建了此方法:

    public static async void InitNotificationsAsync(string userName)
    {
        channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
        string[] subscription = { userName };
    
    
        receivingHub = new NotificationHub("yourappnotificationhub", "Endpoint=sb://yourappnotificationhub-ns.servicebus.windows.net/;....");
    
        var result = await receivingHub.RegisterNativeAsync(channel.Uri, subscription);
    
        // Displays the registration ID so you know it was successful
        if (result.RegistrationId != null)
        {
            channel.PushNotificationReceived += OnPushNotification;
        }
    }
    

在订阅数组中,您可以输入当前登录您的应用程序的登录名或姓名。 填充订阅数组后,必须将其作为 channel Uri 旁边的参数附加到 RegisterNativeAsync 方法。

现在,在您的测试推送通知控制台应用程序中,您可以使用以下方法检查它:

    private static async void SendNotificationAsync()
    {
        NotificationHubClient hub = NotificationHubClient
            .CreateClientFromConnectionString("Endpoint=sb://menotifyappnotificationhub-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=\...", "yourappnotificationhub");
        var toast = @"<toast><visual><binding template=""ToastText02""><text id=""1"">test</text><text id=""2"">Hello</text></binding>  </visual></toast>";
        await hub.SendWindowsNativeNotificationAsync(toast, "User_Name_You_Added_To_String_Array_In_WindowsPhoneApp");
    }

现在,如果您发送推送,它将仅发送给您在 Windows Phone 应用程序中添加到订阅字符串数组中的姓名的人员。

我还粘贴了用于处理应用程序中收到的推送的代码:

    private static void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
    {
        String notificationContent = "";

        switch (e.NotificationType)
        {
            case PushNotificationType.Badge:
                notificationContent = e.BadgeNotification.Content.GetXml();
                break;

            case PushNotificationType.Tile:
                notificationContent = e.TileNotification.Content.GetXml();
                break;

            case PushNotificationType.Toast:

                notificationContent = e.ToastNotification.Content.GetXml();
                //..DO SOME ACTION, FOR EXAMPLE SHOW MESSAGEDIALOG WITH PUSH MESSAGE

                break;

            case PushNotificationType.Raw:
                notificationContent = e.RawNotification.Content;
                break;
        }

        e.Cancel = true;
    }

希望对您有所帮助。

关于azure - Windows 手机 8.1 : Send notification to specified user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263405/

相关文章:

debugging - 在Windows Phone上,当我使用await跳过异步方法调用时, "Step Over"从异步方法退出

sql-server - SQL Azure - 如何从 SQL Azure 中的主数据库中选择 sysdatabases 表?

azure - Azure 上的 Terraform 预配私有(private) VM

Azure 应用程序网关 : Cannot connect to backend server in

iphone - 我的应用程序仅请求推送通知权限,但不请求位置。我该如何解决这个问题?

windows-phone-8 - 有一种方法可以在 Windows Phone 的推送通知中发送自定义数据吗?

ssl - 基本 SSL 证书问题

ios - MQTT-Client-Framework 在后台保持运行

firebase - 如何根据用户属性、用户受众发送 Firebase Cloud Messaging 通知

c# - 如何在 Windows Phone 应用程序 (C#) 中显示上传进度?