【发布时间】:2014-04-27 18:46:18
【问题描述】:
所以我要做的是使用 javascript 将通知推送到 Windows 8 应用程序。
所以在检查了这个网站后,这是我发现的唯一有用的网站。
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202977(v=vs.105).aspx
我明白以下内容:
1- 您需要开发一个请求推送通知服务 URI 的应用程序,然后该 URI 将与推送客户端服务共享。
(此应用程序的代码不需要任何内容,您只需从上面的链接中复制即可)
2- 您需要使用推送客户端服务发送通知(原始通知)
(这里出现了问题:))
代码是针对我不熟悉的 .NET 文件的问题。我尝试将代码从 .NET 转换为 HTML 和 .JS,我将在其中将 ajax 请求发送到给定的 URI。
但是这给了我一个有线错误,不幸的是我找不到使用 .JS 发送通知的示例
所以这里的原始代码基本上定义了一个简单的表单,它将向 URI 发送请求。这与我使用 JQuery Ajax 请求发送的代码之间的唯一区别
原始代码
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendRaw.aspx.cs" Inherits="SendRaw.SendRaw" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Enter URI:</div>
<asp:TextBox ID="TextBoxUri" runat="server" Width="666px"></asp:TextBox>
<br />
<br />
Enter Value 1:<br />
<asp:TextBox ID="TextBoxValue1" runat="server"></asp:TextBox>
<br />
<br />
Enter Value 2:<br />
<asp:TextBox ID="TextBoxValue2" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="ButtonSendRaw" runat="server" onclick="ButtonSendRaw_Click"
Text="Send Raw Notification" />
<br />
<br />
Response:<br />
<asp:TextBox ID="TextBoxResponse" runat="server" Height="78px" Width="199px"></asp:TextBox>
</form>
C# (.NET)
using System.Net;
using System.IO;
using System.Text;
C# (.NET)
protected void ButtonSendRaw_Click(object sender, EventArgs e)
{
try
{
// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
// notifications out to.
string subscriptionUri = TextBoxUri.Text.ToString();
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
// Create an HTTPWebRequest that posts the raw notification to the Microsoft Push Notification Service.
// HTTP POST is the only method allowed to send the notification.
sendNotificationRequest.Method = "POST";
// Create the raw message.
string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root>" +
"<Value1>" + TextBoxValue1.Text.ToString() + "<Value1>" +
"<Value2>" + TextBoxValue2.Text.ToString() + "<Value2>" +
"</root>";
// Set the notification payload to send.
byte[] notificationMessage = Encoding.Default.GetBytes(rawMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-NotificationClass", "3");
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
// Send the notification and get the response.
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string 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.
TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
}
catch (Exception ex)
{
TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
}
我的代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<script src="jquery-2.0.3.min.js"></script>
<script>
$(document).ready(function(e) {
$('#ButtonSendRaw').click(function(){
console.log("Button Pressed");
var subscriptionUri = $('#TextBoxUri').val();
var textbox1 = $('#TextBoxValue1').val();
var textbox2 = $('#TextBoxValue2').val();
var rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root>" +
"<Value1>" + textbox1 + "<Value1>" +
"<Value2>" + textbox2 + "<Value2>" +
"</root>";
console.log("Sending Data");
$.ajax({
url: subscriptionUri,
type: "POST",
data: rawMessage,
contentType:"text/xml",
success: function(response) {
console.log('Sucess');
console.log("login| Response");
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
});
});
</script>
</head>
<body>
Enter URI:<input type="text" ID="TextBoxUri" Width="666px" /> <br />
Enter Value 1: <input type="text" ID="TextBoxValue1" /> <br />
Enter Value 2: <input ID="TextBoxValue2" /> <br />
<button id="ButtonSendRaw">Sending Data</button> <br />
Response: <input ID="TextBoxResponse" Height="78px" Width="199px" /> <br />
</body>
</html>
我从中得到的错误是:
405 (Method Not Allowed)
虽然我正在发送 POST 请求,但 where 方法自动设置为 OPTIONS
任何建议,提前谢谢:)
【问题讨论】:
标签: c# javascript ajax windows-phone-8 push-notification