0
votes

I'm developing small Windows Store App, which uses WNS. I've created command line application, which sends toast messages to WNS. After sent message from console application, my windows store app didn't get any push notification from WNS.

Scenario:

  1. Launch Windows store application, which store URL with token to the file, which comman line appplication gets url and use it while sending toast message to WNS.
  2. Launch console application and it takes url from file and sends created message to WNS.
  3. My win store app doesn't get any push notification from WNS.

Console app passes authentication process with success. After sending toast I got the response from WNS with following headers:

X-WNS-DEVICECONNECTIONSTATUS: connected

X-WNS-NOTIFICATIONSTATUS: received

X-WNS-STATUS: received

X-WNS-MSG-ID: 41C38906780D2A8C

X-WNS-DEBUG-TRACE: DB3WNS4011132

Content-Length: 0

Date: Sat, 15 Feb 2014 17:12:12 GMT

After that method wnsManager_PushNotificationReceived didn't fire. Windows app is associated with Store.

Code from command line application:

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

Code from 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

1 Answers

2
votes

The problem is the syntax of the template: launch=""

should be

launch=""""

Or leave out the lauch parameter.