为什么我无法将 WNS Toast 通知从 C# 发送到 Windows Phone 8.1
本文关键字:Windows Phone WNS 通知 Toast 为什么 | 更新日期: 2023-09-27 17:56:19
我遵循了以下两个指南:https://msdn.microsoft.com/en-us/library/windows/apps/hh202945(v=vs.105).aspxhttps://msdn.microsoft.com/library/windows/apps/xaml/hh868252
生成的代码如下所示。尽管一切看起来都是正确的,但它返回了一个 404 错误。这怎么可能?任何帮助都会延缓我的自杀。
public static void PushToWindows2()
{
try
{
var accessToken = GetAccessToken("Nhz******************XkwX", "ms-app://s-1-15-2-***************-2150981501");
byte[] contentInBytes = Encoding.UTF8.GetBytes("<toast launch='"'"><visual lang='"en-US'"><binding template='"ToastImageAndText01'"><image id='"1'" src='"World'" /><text id='"1'">Hello</text></binding></visual></toast>");
HttpWebRequest request = HttpWebRequest.Create("https://db5.notify.windows.com/?token=awyaaaborhlhub%2bfxeytzjnz****************pftroh5l18sgorvgrkq%3d") as HttpWebRequest;
request.Method = "POST";
request.ContentLength = contentInBytes.Length;
request.ContentType= "text/xml";
request.Headers.Add("X-WindowsPhone-Target", "token");
request.Headers.Add("X-NotificationClass", "1"); ;
request.Headers.Add("X-WNS-Type", "wns/toast");
request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken.AccessToken.ToString()));
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(contentInBytes, 0, contentInBytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
}
catch (Exception ex)
{
Console.Write("EXCEPTION: " + ex.Message);
}
}
[DataContract]
public class OAuthToken
{
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "token_type")]
public string TokenType { get; set; }
}
public static OAuthToken GetOAuthTokenFromJson(string jsonString)
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
{
var ser = new DataContractJsonSerializer(typeof(OAuthToken));
var oAuthToken = (OAuthToken)ser.ReadObject(ms);
return oAuthToken;
}
}
public static OAuthToken GetAccessToken(string secret, string sid)
{
var urlEncodedSecret = HttpUtility.UrlEncode(secret);
var urlEncodedSid = HttpUtility.UrlEncode(sid);
var body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com",
urlEncodedSid,
urlEncodedSecret);
string response;
using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
response = client.UploadString("https://login.live.com/accesstoken.srf", body);
}
return GetOAuthTokenFromJson(response);
}
我也尝试走PushSharp路线,在这种情况下,我收到"设备订阅已过期"错误。这里是:
var config = new WnsConfiguration("424****.*******nts", "ms-app://s-1-15-2-***************1501", "Nhz************XkwX");
// Create a new broker
var wnsBroker = new WnsServiceBroker (config);
wnsBroker.QueueNotification(new WnsToastNotification {
ChannelUri = deviceId,
Payload = XElement.Parse (@"
<toast>
<visual>
<binding template=""ToastText01"">
<text id=""1"">WNS_Send_Single</text>
</binding>
</visual>
</toast>")
});
}
更新:
它与编码无关。我使用了带有 = 和 + 符号的未编码令牌以及编码标记。还是 404
WNS
的 HTTP 404 错误意味着通道 URI 本身已被修改,因此 WNS 无法理解它。从您的代码来看,通道 URI 似乎正在某处降低()ed - 通常令牌以"AwYAAAD"之类的内容开头。尝试删除通道 URI 的小写内容。
此外,当您需要 WNS 时,您似乎正在混合使用 WNS 和 MSDN 文档(尽管这不会导致 404 错误)。这是 WNS,但这是 MPNS 文档。具体而言,这些标头用于 MPNS,WNS 将忽略或失败:
- X-WindowsPhone-Target
- X-通知类
- X 通知状态
- X 订阅状态
- X 设备连接状态
你想要改用这些 WNS 标头。