无法在Windows Phone 7中为推送通知构建httpmessage

时间:2013-04-28 03:31:38

标签: windows-phone-7 windows-phone-8 windows-phone-7.1 windows-phone windows-phone-8-emulator

我正在尝试构建用于发送推送通知(toast type)的httpmessage,但它无法识别以下代码中的方法。我在类库中有这段代码。

在sendNotificationRequest.Headers.Add(“X-NotificationClass”,“2”)中添加方法;

内容长度在sendNotificationRequest.ContentLength = notificationMessage.Length;

流中的

GetRequestStream requestStream = sendNotificationRequest.GetRequestStream()

HttpWebResponse中的GetResponse响应=(HttpWebResponse)sendNotificationRequest.GetResponse();

        HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(channel.ChannelUri.ToString());
        sendNotificationRequest.Method = "POST";
        //Indicate that you'll send toast notifications!
        sendNotificationRequest.ContentType = "text/xml";
        sendNotificationRequest.Headers = new WebHeaderCollection();
        sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
        if (string.IsNullOrEmpty(txtMessage.Text)) return;

        //Create xml envelope
        string data = "X-WindowsPhone-Target: toast\r\n\r\n" +
                "<?xml version='1.0' encoding='utf-8'?>" +
                "<wp:Notification xmlns:wp='WPNotification'>" +
                "<wp:Toast>" +
                "<wp:Text1>{0}</wp:Text1>" +
                "</wp:Toast>" +
                "</wp:Notification>";

        //Wrap custom data into envelope
        string message = string.Format(data, txtMessage.Text);
        byte[] notificationMessage = Encoding.Default.GetBytes(message);

        // Set Content Length
        sendNotificationRequest.ContentLength = notificationMessage.Length;

        //Push data to stream
        using (Stream requestStream = sendNotificationRequest.GetRequestStream())
        {
            requestStream.Write(notificationMessage, 0, notificationMessage.Length);
        }

        //Get reponse for message sending
        HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
        string notificationStatus = response.Headers["X-NotificationStatus"];
        string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
        string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

1 个答案:

答案 0 :(得分:0)

将代码移动到WCF项目后,它已经工作了。