我正在开发小型Windows商店应用程序,它使用WNS。我创建了命令行应用程序,它向WNS发送吐司消息。在从控制台应用程序发送消息后,我的windows商店应用程序没有从WNS收到任何推送通知。
设想情况:
控制台应用程序成功地通过了认证过程。发送祝酒词后,我收到了WNS的回复,标题如下:
X-WNS-DEVICECONNECTIONSTATUS:连接
X-WNS-NOTIFICATIONSTATUS:已收到
X-WNS-现状:已收到
X-WNS-MSG-ID: 41C38906780D2A8C
X-WNS-调试-跟踪: DB3WNS4011132
内容长度:0
日期:2014年2月15日,星期六17:12:12
在这种方法之后,wnsManager_PushNotificationReceived没有开火。Windows应用程序与Store相关联。
命令行应用程序中的代码:
class Program
{
private static string secret = "Client secret";
private static string SID = "Package Security Identifier (SID)";
private static OAuthToken _token;
private static string XmlToastTemplate = @"<toast launch="">
<visual lang=""en-US"">
<binding template=""ToastText01"">
<text id=""1"">Test message</text>
</binding>
</visual>
</toast>";
private static Uri accesTokenuri = new Uri("https://login.live.com/accesstoken.srf");
static void Main(string[] args)
{
if (_token == null)
{
CreateToken();
}
var message = String.Empty;
while(true)
{
Console.WriteLine("Enter toast message");
message = Console.ReadLine();
if (message == "exit") break;
else
{
var uriWithToken = GetUriWithToken();
var wc = HttpWebRequest.Create(uriWithToken) as HttpWebRequest;
wc.Method = "POST";
wc.Headers.Add("X-WNS-Type", "wns/toast");
wc.Headers.Add("X-WNS-RequestForStatus", "true");
wc.Headers.Add("Authorization", String.Format("Bearer {0}", _token.AccessToken));
wc.ContentType = "text/xml";
var byteContent = Encoding.UTF8.GetBytes(XmlToastTemplate);
using (var requestStream = wc.GetRequestStream())
{
requestStream.Write(byteContent,0,byteContent.Length);
}
using (var response = wc.GetResponse() as HttpWebResponse)
{
if (response != null)
{
var statusCode = response.StatusCode;
Console.WriteLine(statusCode);
}
}
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
private static string GetUriWithToken()
{
using(var file = File.OpenText(@"C:\Users\Michas\Pictures\uri.text"))
{
return file.ReadToEnd();
}
}
private static void CreateToken()
{
var encSid = WebUtility.UrlEncode(SID);
var encSecret = WebUtility.UrlEncode(secret);
var body =
String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com",
encSid, encSecret);
var wb = new WebClient();
wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var response = wb.UploadString(accesTokenuri , body);
_token = GetOAuthJSON(response);
}
private static OAuthToken GetOAuthJSON(string json)
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
var ser = new DataContractJsonSerializer(typeof(OAuthToken));
var oAuthToken = (OAuthToken)ser.ReadObject(ms);
return oAuthToken;
}
}
[DataContract]
class OAuthToken
{
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "token_type")]
public string TokenType { get; set; }
}
}windows商店应用程序的代码:
private async void WNSExample()
{
try
{
var wnsManager = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
wnsManager.PushNotificationReceived += wnsManager_PushNotificationReceived;
SaveUriToFile(wnsManager.Uri);
(Exception ex)
{
}
}
private async void SaveUriToFile(string uri)
{
var storageFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync("uri.text", CreationCollisionOption.ReplaceExisting);
using (var stream = await storageFile.OpenStreamForWriteAsync())
{
using (var textWriter = new StreamWriter(stream))
{
textWriter.Write(uri);
textWriter.Flush();
}
}
}发布于 2014-03-02 10:09:42
问题是模板的语法: launch="“
应该是
Launch=“
或者省略lauch参数。
https://stackoverflow.com/questions/21801041
复制相似问题