0
我正在開發使用WNS的小型Windows應用商店應用程序。我創建了命令行應用程序,它向WNS發送Toast消息。從控制檯應用程序發送消息後,我的Windows應用商店應用程序未收到來自WNS的任何推送通知。Windows商店應用程序未收到來自WNS的推送通知
場景:
- 啓動Windows Store應用程序,該商店的網址與令牌的文件,該文件comman線應用項目獲得URL和在發送吐司消息WNS使用它。
- 啓動控制檯應用程序,它從文件中獲取URL並將創建的消息發送給WNS。
- 我的雙贏商店應用程序沒有收到來自WNS的推送通知。
控制檯應用程序通過驗證過程成功。發送吐司後我從WNS與下列標頭的響應:
X-WNS-DEVICECONNECTIONSTATUS:連接
X-WNS-NOTIFICATIONSTATUS:接收
X-WNS-STATUS:接收
X-WNS-MSG-ID:41C38906780D2A8C
X-WNS-DEBUG-TRACE:DB3WNS4011132
的Content-Length:0
日期:星期六,2014年2月15日17點12分12秒GMT
該方法wnsManager_PushNotificationReceived不火之後。 Windows應用程序與商店相關聯。從命令行應用程序
代碼:從Windows商店應用
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; }
}
}
代碼:
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();
}
}
}