c# - 即将到来的 GCM 响应是 : Error=NotRegistered

标签 c# android asp.net push-notification google-cloud-messaging

这是我为 GCM 创建的示例服务器。

class Program2
{
    public static string SenderId = "318115091714";
    public static string RegistrationID = "APA91bF9hn6VeessobraNuauBcrFdlJ9eH1eVb44FAQ2oawerBeFWS48IEIFTPo8fdvWm93hwFY0tKszpPuSObPbTqgW-za1RLhCw-GDCn4JQZLQ-CmGwnnr6F5X8gYhNa2DNvFhCEM7HNgvdxtcnBqVX0dVnEynXQ";
    public static string ApiKey = "AIzaSyAl2HdB4bbukkcmJwoxUmhof15IAiuJ16U";
    public static string Message = "Testing GCM Server";
    public static string ApplicationId = "com.google.android.gcm.demo.app";

    /// <summary>
    /// Main method
    /// </summary>
    public static void Main(string[] args)
    {
        try
        {
            Program2 objProgram2 = new Program2();

            Console.WriteLine("\nPlease wait while GCM server is processing...");
            string Text = objProgram2.SendMessage();
            Console.WriteLine("\nSendMessage Response: " + Text);

            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n" + ex.Message);
            Console.WriteLine("\n" + ex.StackTrace);
            Console.ReadLine();
        }
    }

    /// <summary>
    /// Send Push Message to Device
    /// </summary>
    public string SendMessage()
    {
                                                       //-- Create Query String --//
        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=" + RegistrationID + "";
        //Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                                                    //-- Create GCM Request Object --//
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        Request.Method = "POST";
        Request.KeepAlive = false;
        Request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        Request.Headers.Add(string.Format("Authorization: key={0}", ApiKey));
        Request.Headers.Add(string.Format("Sender: id={0}", SenderId));
        Request.ContentLength = byteArray.Length;

                                         //-- Delegate Modeling to Validate Server Certificate --//
        ServicePointManager.ServerCertificateValidationCallback += delegate(
                    object
                    sender,
                    System.Security.Cryptography.X509Certificates.X509Certificate
                    pCertificate,
                    System.Security.Cryptography.X509Certificates.X509Chain pChain,
                    System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
        {
            return true;
        };

                                            //-- Create Stream to Write Byte Array --// 
        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

                                                    //-- Post a Message --//
        WebResponse Response = Request.GetResponse();
        HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
        if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
        {
            return "Unauthorized - need new token";

        }
        else if (!ResponseCode.Equals(HttpStatusCode.OK))
        {
            return "Response from web service isn't OK";
            //Console.WriteLine("Response from web service is not OK :");
            //Console.WriteLine(((HttpWebResponse)Response).StatusDescription);
        }

        StreamReader Reader = new StreamReader(Response.GetResponseStream());
        string responseLine = Reader.ReadLine();
        Reader.Close();

        return responseLine;
    }
}

在使用这些有效值和键运行它后,我收到了这个响应。

Please wait while GCM server is processing...

SendMessage Response: Error=NotRegistered

我收到 Error=NotRegistered。 Android 开发人员指南中甚至没有指定此响应。我收到此回复的原因应该是什么?谁能帮我解决这个问题?提前致谢。

最佳答案

我已经找到了它发生的原因。可能有六种类型的响应。以下是响应列表及其含义。

{ "message_id": "1:0408" } - success
{ "error": "Unavailable" } - should be resent
{ "error": "InvalidRegistration" } -  had an unrecoverable error (maybe the value got corrupted in the database)
{ "message_id": "1:1516" } - success
{ "message_id": "1:2342", "registration_id": "32" } - success, but the registration ID should be updated in the server database
{ "error": "NotRegistered"} - registration ID should be removed from the server database because the application was uninstalled from the device

我收到错误消息 6。使用新的发件人 ID、注册 ID 和 API key ,我的上述代码正在运行。

关于c# - 即将到来的 GCM 响应是 : Error=NotRegistered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11682539/

相关文章:

android - "zipAlignExe' 不存在。”在 Android Studio 上运行我的项目时出错

java - 划掉文字

c# - 在单独的后台线程与进程中运行长时间后台任务

c# - 如何在 C# 中停止 ffmpeg 视频录制?

C# - 具有属性和节点值的 Xml 元素

c# - 安卓手机浏览器检测

android - 在 WebView 中拦截 "file:"scheme 请求

mysql - .NET 表单 MySQLConnection 404 错误

asp.net - "Create App Service"对话未检索信息,创建按钮被禁用

c# - 使用部分名称搜索 UserPrincipal