json - GCM request in Asp.net -


i have reffered link sending gcm request , working fine. gcm-push-notification-with-asp-net

i referred 1 more link post json how-to-post-json-to-the-server

on basis of second link have tried following code.

        var httprequest = (httpwebrequest)webrequest.create("https://gcm-http.googleapis.com/gcm/send");         httprequest.contenttype = "application/json";         httprequest.method = "post";         httprequest.headers.add(string.format("authorization: key={0}", gcm.apikey));         httprequest.headers.add(string.format("sender: id={0}", gcm.projectno));          using (var streamwriter = new streamwriter(httprequest.getrequeststream()))         {             string json = new javascriptserializer().serialize(new gcmvalues             {                 delay_while_idle = false,                 priority = "high",                 registration_id = regid,                 data = new messagesvalues                 {                     message = message                 }             });              streamwriter.write(json);             streamwriter.flush();             streamwriter.close();         }          var httpresponse = (httpwebresponse)httprequest.getresponse();         using (var streamreader = new streamreader(httpresponse.getresponsestream()))         {             var result = streamreader.readtoend();         } 

properties have used

public class gcmvalues {     public object data { get; set; }     public bool delay_while_idle { get; set; }     public string priority { get; set; }     public string registration_id { get; set; } } public class messagesvalues {     public string message { get; set; }     public datetime? time { get; set; } } 

the problem facing @ line var httpresponse =(httpwebresponse)httprequest.getresponse();

i getting response of bad request. went wrong or done pass values in json format gcm post request. in advance.

you need registration_ids (plural) not registration_id (singular) in json. array of strings , not single string value.

if check gcm documentation gives json options.

elsewhere documentation details how use to if have 1 token send notification to, in example:

  {     "to" : "bk3rnwte3h0:ci2k_hhwgipodkcizvvdmexudfq3p1...",     "notification" : {       "body" : "great match!",       "title" : "portugal vs. denmark",       "icon" : "myicon"     }   } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -