Windows应用商店应用不会收到来自WNS的推送通知

时间:2014-02-15 17:28:05

标签: c# windows-store-apps windows-8.1

我正在开发使用WNS的小型Windows应用商店应用。我创建了命令行应用程序,它向WNS发送Toast消息。从控制台应用程序发送消息后,我的Windows应用商店没有从WNS获得任何推送通知。

情景:

  1. 启动Windows商店应用程序,该应用程序将带有令牌的URL存储到文件中,comman line appplication获取url并在向WNS发送Toast消息时使用它。
  2. 启动控制台应用程序,它从文件中获取url并将创建的消息发送到WNS。
  3. 我的win store应用程序未收到来自WNS的任何推送通知。
  4. 控制台应用程序成功通过身份验证过程。发送toast后,我收到了来自WNS的回复,其中包含以下标题:

    X-WNS-DEVICECONNECTIONSTATUS:已连接

    X-WNS-NOTIFICATIONSTATUS:已收到

    X-WNS-STATUS:已收到

    X-WNS-MSG-ID:41C38906780D2A8C

    X-WNS-DEBUG-TRACE:DB3WNS4011132

    内容长度:0

    日期:2014年2月15日星期六17:12:12 GMT

    在那个方法之后,wnsManager_PushNotificationReceived没有触发。 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; }
            }
        }
    

    来自windows store app的代码:

        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();
                    }
                }
    }
    

1 个答案:

答案 0 :(得分:2)

问题是模板的语法:launch =“”

应该是

发射= “” “”

或者省略lauch参数。