Search code examples
androidandroid-c2dm

C2DM Error=MissingRegistration i already put registration_id to request


i am working to sent a push message to android phone from my asp.net project and i already put register_id in my code but i handle an error Missing registration, i didnt understand its reason.

Here is my code:

 public void SendMessage(string registrationId, string data, string sAuth)
{
    string collapseKey = Guid.NewGuid().ToString("n");

    ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);

    string url = "https://android.apis.google.com/c2dm/send?";
    url = url + HttpUtility.UrlEncode("registration_id=" + registrationId + "&collapse_key=" + collapseKey + "&data.payload=" + data);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = "POST";
    request.Headers.Add("Authorization", "GoogleLogin auth=" + sAuth);
    //request.ContentLength = 0;  

    string encoded = HttpUtility.UrlEncode(url);

    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] buffer = encoding.GetBytes(encoded);

    Stream newStream = request.GetRequestStream();
    newStream.Write(buffer, 0, buffer.Length);
    newStream.Close();

    //Reading return Response

    try
    {
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream);
            string value = sr.ReadToEnd().ToString();
            Label1.Text = value;
            //Response.Write(sr.ReadToEnd());
            sr.Close();
            resStream.Close();
        }
    }
    catch (Exception ex)
    {
        throw (ex);
    }
}

Solution

  • İ solve the problem like this;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://android.clients.google.com/c2dm/send");
            request.Method = "POST";
            request.KeepAlive = false;
    
            NameValueCollection postFieldNameValue = new NameValueCollection();
            postFieldNameValue.Add("registration_id", registrationId);
            postFieldNameValue.Add("collapse_key", "1");
            postFieldNameValue.Add("delay_while_idle", "0");
            postFieldNameValue.Add("data.message", message);
    
            string postData = GetPostStringFrom(postFieldNameValue);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            request.ContentLength = byteArray.Length;
    
            request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + authTokenString);
    
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            //savas
            ServicePointManager.ServerCertificateValidationCallback += delegate(
                        object
                        sender,
                        System.Security.Cryptography.X509Certificates.X509Certificate
                        pCertificate,
                        System.Security.Cryptography.X509Certificates.X509Chain pChain,
                        System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
            {
                return true;
            };
            ////***********
    
            WebResponse response = request.GetResponse();
            HttpStatusCode responseCode = ((HttpWebResponse)response).StatusCode;
            if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
            {
                Console.WriteLine("Unauthorized - need new token");
            }
            else if (!responseCode.Equals(HttpStatusCode.OK))
            {
                Console.WriteLine("Response from web service not OK :");
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            }
    
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string responseLine = reader.ReadLine();
            reader.Close();