【问题标题】:How to send push notifications windows phone 8 app?如何发送推送通知 windows phone 8 应用程序?
【发布时间】:2015-05-26 13:30:38
【问题描述】:

我是 windows phone 的新手。我正在开发一个应用程序。在这个应用程序中我想发送 pushnotifications。如何使用 c# 在 windows phone 8 中发送和接收 pushnotifications。请帮助我。

public string SendNotificationToWindows(string message, string notificationId)
        {
            try
            {

                //string subscriptionUri = "http://db3.notify.live.net/throttledthirdparty/01.00/AwYAAACKB3Noan4l%2bojXM5%2f3TDodPTegXbZxtTAzRktj3eWFOYmjjN1FPIdkuduXrwYZByFKLxy1gXy8rCmf1FSM6GH92rva7ecbQ%2b1%2bnGYxLWxoAI0GL03fZbV29p%2fu%2fJYrHQI%3d";
                 string subscriptionUri = "http://db3.notify.live.net/throttledthirdparty/01.00/aHR0cHM6Ly9zaW4ubm90aWZ5LndpbmRvd3MuY29tLz90b2tlbj1Bd1lBQUFEQjE1TzJMQWMlMmZBQldlUlpQendHMlglMmJRNWlPbzVUOVF3UUtXeUFQJTJic2clMmZFREhuSHM0bDBVN2tFN2prSXVJYU1hWEZIdmJYR2t6cEpQJTJiaCUyYldJSVJFTjBSd244TzJRNFV5RUs0OFJKZDdLSWJPeXVUMXFNWVNwa0Y3bmlBak5kZmslM2Q=";

                //string subscriptionUri = "https://hk2.notify.windows.com/?token=AwYAAACKB3Noan4l%2bojXM5%2f3TDodPTegXbZxtTAzRktj3eWFOYmjjN1FPIdkuduXrwYZByFKLxy1gXy8rCmf1FSM6GH92rva7ecbQ%2b1%2bnGYxLWxoAI0GL03fZbV29p%2fu%2fJYrHQI%3d";


                //string subscriptionUri = "http://sn1.notify.live.net/throttledthirdparty/01.00/aHR0cHM6Ly9zaW4ubm90aWZ5LndpbmRvd3MuY29tLz90b2tlbj1Bd1lBQUFEckVzRmdhR2phMXQ1aVo5MGdvRzAzejR5cE1SJTJiMHIwR2ZQc0Q0U0xzYnJOY2V3JTJmdU5pek1kZER4ZG9UdE5CM05PbjQ4dU9yUktzakN0U2JJa2lObmdBQVljQzdScDZ0blRBZlBDWjB4OWlZMDJRSDF3JTJieHM1ZzVMSTlWSXdGZWslM2Q=";

              //  string subscriptionUri = "https://sin.notify.windows.com/?token=AwYAAADrEsFgaGja1t5iZ90goG03z4ypMR%2b0r0GfPsD4SLsbrNcew%2fuNizMddDxdoTtNB3NOn48uOrRKsjCtSbIkiNngAAYcC7Rp6tnTAfPCZ0x9iY02QH1w%2bxs5g5LI9VIwFek%3d";




                var sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);

                // Create an HTTPWebRequest that posts the toast notification to the Microsoft Push Notification Service.
                // HTTP POST is the only method allowed to send the notification.
                sendNotificationRequest.Method = "POST";

                // The optional custom header X-MessageID uniquely identifies a notification message. 
                // If it is present, the same value is returned in the notification response. It must be a string that contains a UUID.
                // sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>");

                // Create the toast message.
                var toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                   "<wp:Notification xmlns:wp=\"WPNotification\">" +
                      "<wp:Toast>" +
                           "<wp:Text1>" + message + "</wp:Text1>" +
                      "</wp:Toast> " +
                   "</wp:Notification>";
                // Set the notification payload to send.
                byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);

                // Set the web request content length.
                sendNotificationRequest.ContentLength = notificationMessage.Length;
                sendNotificationRequest.ContentType = "text/xml";
                sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
                sendNotificationRequest.Headers.Add("X-NotificationClass", "2");


                using (var requestStream = sendNotificationRequest.GetRequestStream())
                {
                    requestStream.Write(notificationMessage, 0, notificationMessage.Length);
                }

                // Send the notification and get the response.
                var response = (HttpWebResponse)sendNotificationRequest.GetResponse();
                var notificationStatus = response.Headers["X-NotificationStatus"];
                var notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                var deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

                // Display the response from the Microsoft Push Notification Service.  
                // Normally, error handling code would be here. In the real world, because data connections are not always available,
                // notifications may need to be throttled back if the device cannot be reached.
                var rep = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
                return rep;
            }
            catch (Exception ex)
            {
                return ex.ToString();
                // TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
            }

这是我在 uri 上向该服务发送的服务,但我不知道何时响应它会抛出异常“远程服务器返回错误:(400)错误请求。”但我不知道是什么uri 的类型将给出。在此 uri 中发送一个密钥,但将发送哪种类型的密钥请任何人回复我

【问题讨论】:

  • 你试过谷歌吗?
  • 是的@glorfindel。但我找不到任何有用的答案。请给我任何示例
  • 您学习了哪个教程?哪里出了问题?
  • @glorfindel。请找到上面的代码
  • 你最好使用像 PushSharp 这样的公共库(就像下面的 @Rohit 建议的那样。)不要重新发明轮子!

标签: windows windows-phone-8 push-notification


【解决方案1】:

您可以查看PushSharp 它似乎已针对 Windows Phone 8 中的新通知负载格式进行了更新。Here 是用于从服务器发送推送通知的服务器代码,在此 link 您可以找到Windows Phone 的实现。

基本上,您的应用可能在 Windows Phone 8.1 中使用两种类型的推送通知。

  1. MPNS: Microsoft Push Notification Service

  2. WNS: Windows Notification Service.

MPNS 是在 Windows Phone 7 和 8 中使用的旧式通知服务。WNS 是 Windows 8 式通知,适用于专门针对 Windows Phone 8.1 的应用程序。 (Windows Phone Silverlight 8.1 应用和 Windows Phone 8.1(Windows 运行时)应用。

PushSharp 似乎支持两种类型的通知服务,PushSharp.WindowsPhone 库中的 MPNS 和 PushSharp.Windows 库中的 WNS。

同样值得一试 Azure。 这是相同的教程 => Send push notifications to authenticated users

【讨论】:

  • 感谢 rohit,但我在 PushSharp 上面找不到任何链接,它会显示空白页面。请给我示例代码。实际上我使用的是 windows 运行时而不是 silverlight 模板。
  • 请找到答案的更新。否则检查此链接 => github.com/Redth/PushSharp/blob/master/Client.Samples/…
【解决方案2】:

以下是push notification for windows phone 所需的步骤。

  1. 您的应用从推送客户端服务请求推送通知 URI。
  2. Push 客户端服务与 Microsoft Push Notification Service (MPNS) 协商,MPNS 向 Push 客户端服务返回一个通知 URI。
  3. Push 客户端服务将通知 URI 返回到您的应用程序。
  4. 然后您的应用可以将通知 URI 发送到您的云服务。
  5. 当您的云服务有信息要发送到您的应用时,它会使用通知 URI 向 MPNS 发送推送通知。
  6. MPNS 将推送通知路由到您的应用。

请参阅this MSDN 页面以详细了解推送通知。

【讨论】:

    【解决方案3】:

    我尝试使用以下代码手动发送通知:

    void SendToastMessage(string pushUri, string message)
            {
                HttpWebRequest request =
                    (HttpWebRequest)WebRequest.Create(pushUri);
                request.ContentType = "text/xml";
                request.Method = "POST";
                request.Headers.Add("X-MessageID", Guid.NewGuid().ToString());
                request.Headers.Add("X-NotificationClass", "2");
                request.Headers.Add("X-WindowsPhone-Target", "toast");
                string toastMessage = @"<?xml version=""1.0"" encoding=""utf-8""?>
                              <wp:Notification
                                  xmlns:wp=""WPNotification"">
                                  <wp:Toast>
                                    <wp:Text1>{0}</wp:Text1>
                                    <wp:Text2>{1}</wp:Text2>
                                  </wp:Toast>
                              </wp:Notification>";
                string toastXml = string.Format(toastMessage,
                                      "sample:",
                                      message);
                byte[] notificationMessage = Encoding.UTF8.GetBytes(toastXml);
                request.ContentLength = notificationMessage.Length;
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(notificationMessage,
                              0,
                              notificationMessage.Length);
                }
                try
                {
                    HttpWebResponse response =
                    (HttpWebResponse)request.GetResponse();
    
                    return;
                }
                catch (Exception ex)
                {
                    return;
                }
            }
    

    一年前这段代码对我来说非常好用,现在仍然如此。但请确保您的 pushUri 正确。

    【讨论】:

    • 感谢@deeiip,但我不知道你会发送什么类型的 pushuri。请给我任何示例 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
    相关资源
    最近更新 更多