Jigar Patel
Jigar Patel

Reputation: 1

GCM returning Missing Registration Error

I am using c# webservices to pass registration ids to GCM. I have two android phones and I have got valid registration ids.When i pass them individually GCm does not return any error and devices are notified.But when i try to pass them together as JSON I am getting error as "Error = missing registration".

Following is the code of my webservice that passes registtration ids to GCM..

 public string Notify(string deviceId, string message)
    {

        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
//        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
        tRequest.Headers.Add(HttpRequestHeader.Authorization, "key=AIzaSyCUJX_f_NZzYbigWMDK9U5vkzZ9mPiK8jA");
       tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
       int count = 2;

           SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["Login"]);
           conn.Open();

           string send_notification = "select RegId from REG_ID ";//where RegId <> '"+deviceId+"'";
           SqlCommand cmd2 = new SqlCommand(send_notification, conn);
           DataSet ds2 = new DataSet();
           SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);
           adapter2.Fill(ds2);
           int count1 = ds2.Tables[0].Rows.Count;
           registration[] r = new registration[count1];
           for (int i = 0; i < count1; i++)
           {
               r[i] = new registration();
               r[i].registration_ids = ds2.Tables[0].Rows[i]["RegId"].ToString();
           } 
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJson = js.Serialize(r);
        string postData = /*"collapse_key=score_update&time_to_live=108&delay_while_idle=1&*/"data.message=" + message + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id= "+ deviceId + "";
        Console.WriteLine(strJson);
        Byte[] byteArray = Encoding.UTF8.GetBytes(strJson);
        tRequest.ContentLength = byteArray.Length;

        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = tRequest.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();

        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return sResponseFromServer;
    }

I am new to developing and may be missing something very simple. Any help is appreciated..Thanks in advance

Upvotes: 0

Views: 6864

Answers (1)

Eran
Eran

Reputation: 393821

Your JSON should look like this :

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "registration_ids": ["4", "8", "15", "16", "23", "42"]
}
  • The code that you posted has a mix of the JSON format and the plain text format.
  • IF you want to send multiple message in one request, you should change your content type to application/json.
  • It's not clear from your code what your strJson contains, so I can't say whether it's valid.
  • Your postData variable seems to belong to the plain text format, but you don't use it anyway.

Upvotes: 2

Related Questions