C2DM Error=MissingRegistration i already put registration_id to request

早过忘川 提交于 2019-12-25 02:16:14

问题


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);
    }
}

回答1:


Can try this code. I think the problem comes from url and request parameters.

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";
    string params = 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;  

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

    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);
    }
}



回答2:


If it's first time running your application on your device, then you have to handle first registration id. More details can be found here : http://code.google.com/android/c2dm/index.html

If it's not the case, then you have to take in consideration this issue: "The C2DM server periodically refreshes registration IDs." Witch means that you have to handle each refresh of your registration id.

Hope it helps.




回答3:


İ 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();



回答4:


In this method SendMessage(string registrationId, string data, string sAuth) not get notification in Andriod Device and No any error Message , What to do ?



来源:https://stackoverflow.com/questions/8339328/c2dm-error-missingregistration-i-already-put-registration-id-to-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!