question

tassine avatar image
tassine asked

sending a message from server in c#

I m trying to send a message from my server, like it's specified here: https://developer.amazon.com/sdk/adm/sending-message.html#RequestFormat , according the same Request Format. to do that I tried with this code : private void sendNotification(String registrationID,String message, String title,String accessToken) { string url = " https://api.amazon.com/messaging/registrations/"+ registrationID +"/messages"; var client = new HttpClient(); //set Request headers client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", accessToken); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("X-Amzn-Type-Version", "com.amazon.device.messaging.ADMMessage@1.0"); client.DefaultRequestHeaders.Add("X-Amzn-Accept-Type", "com.amazon.device.messaging.ADMSendResult@1.0"); //the content of the message body var content = new Dictionary (); content.Add("consolidationKey", "SyncNow"); content.Add("expiresAfter", 86400); var data = new Dictionary (); data.Add("message", message); data.Add("title", title); content.Add("data", data); var result = client.PostAsJsonAsync >(url, data).Result; } As response I get StatusCode:400,ReasonPhrase:'Bad Request', I don't know why ?? someone can help me, please !! For detail about result wich I got: result {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { x-amzn-RequestId: 1b943a1c-fe94-11e2-b963-71a537223b43 Vary: Accept-Encoding Vary: User-Agent Cneonction: close Date: Tue, 06 Aug 2013 12:31:24 GMT Content-Length: 34 Content-Type: application/json }} System.Net.Http.HttpResponseMessage result.RequestMessage {Method: POST, RequestUri: ' https://api.amazon.com/messaging/registrations/(REGISTRATION_ID_FOR_DESTINATION_APP_INSTANCE)/messages', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], [System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Headers: { Authorization: Bearer (MY_ACCESS_TOKEN) Accept: application/json X-Amzn-Type-Version: com.amazon.device.messaging.ADMMessage@1.0 X-Amzn-Accept-Type: com.amazon.device.messaging.ADMSendResult@1.0 Content-Type: application/json; charset=utf-8 Content-Length: 98 }} System.Net.Http.HttpRequestMessage
amazon device messaging
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

tassine avatar image
tassine answered
Hi everybody, I found an other solution to send a message, that work perfectly. private void sendNotification(String registrationID,String message,String title,String accessToken) { HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(" https://api.amazon.com/messaging/registrations/" + registrationID + "/messages"); Encoding encoding = new UTF8Encoding(); string postData = "{\"data\":{\"message\":\""+message+"\",\"title\":\""+title+"\"},\"consolidationKey\":\"Some Key\",\"expiresAfter\":86400}"; byte[] data = encoding.GetBytes(postData); httpWReq.ProtocolVersion = HttpVersion.Version11; httpWReq.Method = "POST"; httpWReq.ContentType = "application/json";//charset=UTF-8"; httpWReq.Headers.Add("X-Amzn-Type-Version", "com.amazon.device.messaging.ADMMessage@1.0"); httpWReq.Headers.Add("X-Amzn-Accept-Type", "com.amazon.device.messaging.ADMSendResult@1.0"); httpWReq.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken); httpWReq.ContentLength = data.Length; Stream stream = httpWReq.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse(); string s=response.ToString(); StreamReader reader = new StreamReader(response.GetResponseStream()); String responseStream = ""; String temp = null; while ((temp = reader.ReadLine()) != null) { responseStream += temp; } }
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.