【问题标题】:Windows Phone 8.1: Send notification to specified userWindows Phone 8.1:向指定用户发送通知
【发布时间】:2015-02-01 13:51:56
【问题描述】:

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

附言。对不起我的英语;)

【问题讨论】:

    标签: azure push-notification windows-phone-8.1


    【解决方案1】:

    我想向您展示我创建的用于我的应用程序的示例。 我还使用 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;
          }
      }
      

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

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

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

    我希望它会有所帮助。

    【讨论】:

    • 它类似于在旧版本的 WP 8.0 (Silverlight) 中使用。您只需要处理 HttpNotificationChannel 而不是 PushNotificationChannelManager 等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多