title: Getting (400) Bad Request. when trying to send GCM Message
body: <p>I am trying to send message to GCM Server through C#.net</p>
  
  <p>I found a good example that I am trying to use</p>
  
  <p><a href="http://www.codewithasp.net/2015/11/send-message-gcm-c-sharp-single-multiple.html" rel="nofollow noreferrer">http://www.codewithasp.net/2015/11/send-message-gcm-c-sharp-single-multiple.html</a></p>
  
  <p>The problem is that I get "The remote server returned an error: (400) Bad Request." error whenever I try to Get the response from this line </p>
  
  <pre><code>            WebResponse wResponse = wRequest.GetResponse();
  </code></pre>
  
  <p>I tried to find what the prblem is, I changed the API code from Server to Android, to Browser, and the result is either (400) Bad request or (401) Unauthorized.</p>
  
  <p>Not sure what I am doing wrong here, it looked very simple and straight forward in the example.</p>
  
  <p>Help is much appreciated</p>
  
  <p>Here is The code</p>
  
  <pre><code>        NotificationManager nm = new NotificationManager();
          List&lt;string&gt; ls = new List&lt;string&gt;(new string[] { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" });
  
          nm.SendNotification(ls, " Hi.. This is a test","Hi.. Title",1);
  </code></pre>
  
  <p>and here is the class</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Script.Serialization;
  using System.Net;
  using System.Text;
  using System.IO;
  
  namespace WebApplication1
  {
  
      public class NotificationManager
      {
          private class NotificationMessage
          {
              public string Title;
              public string Message;
              public long ItemId;
          }
  
          public NotificationManager()
          {
              //
              // TODO: Add constructor logic here
              //
          }
  
          public string SendNotification(List&lt;string&gt; deviceRegIds, string message, string title, long id)
          {
              try
              {
                  string regIds = string.Join("\",\"", deviceRegIds);
  
                  string AppId = "AIzaSyXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                  var SenderId = "1234567890";
  
                  NotificationMessage nm = new NotificationMessage();
                  nm.Title = title;
                  nm.Message = message;
                  nm.ItemId = id;
  
                  var value = new JavaScriptSerializer().Serialize(nm);
                  WebRequest wRequest;
                  wRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
                  wRequest.Method = "post";
                  wRequest.ContentType = " application/json;charset=UTF-8";
                  wRequest.Headers.Add(string.Format("Authorization: key={0}", AppId));
  
                  wRequest.Headers.Add(string.Format("Sender: id={0}", SenderId));
  
                  string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\": { \"message\" : " + "\"" + value + "\",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\"},\"registration_ids\":[\"" + regIds + "\"]}";
  
                  Byte[] bytes = Encoding.UTF8.GetBytes(postData);
                  wRequest.ContentLength = bytes.Length;
  
                  Stream stream = wRequest.GetRequestStream();
                  stream.Write(bytes, 0, bytes.Length);
                  stream.Close();
  
                  WebResponse wResponse = wRequest.GetResponse();
  
                  stream = wResponse.GetResponseStream();
  
                  StreamReader reader = new StreamReader(stream);
  
                  String response = reader.ReadToEnd();
  
                  HttpWebResponse httpResponse = (HttpWebResponse)wResponse;
                  string status = httpResponse.StatusCode.ToString();
  
                  reader.Close();
                  stream.Close();
                  wResponse.Close();
  
                  if (status == "")
                  {
                      return response;
                  }
                  else
                  {
                      return "";
                  }
              }
              catch( Exception ex)
              {
                  return ex.ToString();
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is how I get the API ID and Sender ID</p>
  
  <p><a href="https://i.stack.imgur.com/gzlet.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gzlet.png" alt="Sender ID"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ks7Bw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ks7Bw.png" alt="API ID"></a></p>