【问题标题】:I've Got This Response From The GCM Server {"success":1} But the Notification not Come to the Device我从 GCM 服务器收到此响应 {"success":1} 但通知未到达设备
【发布时间】:2012-12-05 11:18:57
【问题描述】:

我正在尝试通过推送通知来通知设备

我收到了来自GCM 服务器的回复

{"multicast_id":8594338261894783737,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1355822022916886%8ae6056ef9fd7ecd"}]}

但仍然没有收到通知

知识 --> "success":1

但我认为这里有问题 --> "canonical_ids":0


这是我的代码...

 private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
 {
     ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

     //
     //  MESSAGE CONTENT
     byte[] byteArray = Encoding.UTF8.GetBytes(postData);

     //
     //  CREATE REQUEST
     HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
     Request.Method = "POST";
     Request.KeepAlive = false;
     Request.ContentType = postDataContentType;
     Request.Headers.Add(HttpRequestHeader.Authorization, string.Format("key={0}",apiKey));
     Request.ContentLength = byteArray.Length;

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

     try
     {
         WebResponse Response = Request.GetResponse();
         HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
         if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
         {
          Label1.Text = "Unauthorized - need new token";

         }
         else if (!ResponseCode.Equals(HttpStatusCode.OK))
         {
             Label1.Text = "Response from web service isn't OK";
         }

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

         return responseLine;
     }
     catch (Exception e)
     {
         return "error";
     }
    // return "error";
 }

我调用这个方法使用

     string deviceId = "APA91bHomX3zb6Y87fb4GAjyj8zIaI-tt1n6ZFmgtmu16nmLW7ntwnOyv4BXMH7RzQWk3JrKdLjttJMxKzvpFd3Kmrid_RzsC3zR46GLJGiZKERXOSIR8fYReBEfz1f0G_FIm5bPttWUBDwz9jPuF2lS8RQh-0DKbw";
     string message = "some test message";
     string tickerText = "example test GCM";
     string contentTitle = "content title GCM";
     string postData =
     "{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
       "\"data\": {\"tickerText\":\"" + tickerText + "\", " +
                  "\"contentTitle\":\"" + contentTitle + "\", " +
                  "\"message\": \"" + message + "\"}}";

     Label1.Text = SendGCMNotification("AIzaSyBEvtrtbbfy2-p2zS8Zi8DweZiRy8M-nZc", postData);

提前致谢

【问题讨论】:

    标签: c# android push-notification google-cloud-messaging


    【解决方案1】:

    查看 GCM 文档中的响应格式:http://developer.android.com/google/gcm/gcm.html#response

    success     Number of messages that were processed without an error.
    

    我理解这意味着 GCM 能够处理该消息,这并不意味着该消息已成功发送到设备。 (例如,设备可能离线,稍后会收到,但消息已成功处理)。

    "canonical_ids":0 并不意味着有错误,这意味着没有需要更新其 ID 的设备。您可以在此处阅读有关规范 ID 的更多信息:http://developer.android.com/google/gcm/adv.html#canonical

    在服务器端,只要应用程序运行良好, 一切都应该正常工作。但是,如果应用程序中的错误 触发同一设备的多个注册,可能很难 协调状态,您最终可能会收到重复的消息。

    GCM 提供了一种称为“规范注册 ID”的工具,可以轻松地 从这些情况中恢复过来。定义了一个规范的注册 ID 作为您的应用程序请求的最后一次注册的 ID。 这是服务器在向服务器发送消息时应使用的 ID 设备。

    如果您稍后尝试使用其他注册方式发送消息 ID,GCM 会照常处理请求,但会包含 的registration_id字段中的规范注册ID 回复。确保替换存储在您的注册 ID 具有此规范 ID 的服务器,因为最终您使用的 ID 将 停止工作。

    我建议向您的客户端添加一些日志记录代码,以确保您没有收到消息。特别是在您的 GCMIntentService 类的 onMessage() 方法中。

    【讨论】:

    • 正是我需要的,问题出在onMessage()方法,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多