我从 GCM 服务器收到了这个响应 {“success":1} 但通知没有到达设备 [英] I've Got This Response From The GCM Server {"success":1} But the Notification not Come to the Device

查看:12
本文介绍了我从 GCM 服务器收到了这个响应 {“success":1} 但通知没有到达设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我收到了来自 GCM 服务器的响应

{"multicast_id":8594338261894783737,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1355822022916886ef%89600}

还是没有收到通知

知道--> "success":1

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

<小时>

这是我的代码...

 私有字符串 SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json"){ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);////消息内容byte[] byteArray = Encoding.UTF8.GetBytes(postData);////创建请求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;流数据流 = Request.GetRequestStream();dataStream.Write(byteArray, 0, byteArray.Length);数据流.关闭();尝试{WebResponse 响应 = Request.GetResponse();HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden)){Label1.Text = "未授权 - 需要新令牌";}否则 if (!ResponseCode.Equals(HttpStatusCode.OK)){Label1.Text = "来自网络服务的响应不正常";}StreamReader Reader = new StreamReader(Response.GetResponseStream());字符串 responseLine = Reader.ReadToEnd();Reader.Close();返回响应行;}捕获(例外 e){返回错误";}//返回错误";}

我调用这个方法使用

<预> <代码>串的DeviceID = APA91bHomX3zb6Y87fb4GAjyj8zIaI-tt1n6ZFmgtmu16nmLW7ntwnOyv4BXMH7RzQWk3JrKdLjttJMxKzvpFd3Kmrid_RzsC3zR46GLJGiZKERXOSIR8fYReBEfz1f0G_FIm5bPttWUBDwz9jPuF2lS8RQh-0DKbw";string message = "一些测试消息";stringtickerText = "示例测试 GCM";string contentTitle = "内容标题 GCM";字符串 postData ="{ "registration_ids": [ "" + deviceId + "" ], " +""数据": {"tickerText":"" + tickerText + "", " +""contentTitle":"" + contentTitle + "", " +""message": "" + message + ""}}";Label1.Text = SendGCMNotification("AIzaSyBEvtrtbbfy2-p2zS8Zi8DweZiRy8M-​​nZc", postData);

提前致谢

解决方案

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

success 已处理且无错误的消息数.

我理解的意思是 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() 方法中.

I am trying to Notify the Device by the Push Notification

I Received this Response From THE GCM Server

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

But Still Not get the Notification

With Knowledge That --> "success":1

But I Think there is something Wrong Here --> "canonical_ids":0


This Is My Code ...

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

and i call this method using

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

Thanks In Advance

解决方案

Take a look at the Response format in the GCM documentation: http://developer.android.com/google/gcm/gcm.html#response

success     Number of messages that were processed without an error.

What I understand that to mean is that GCM was able to process that message, it does not mean that the message was successfully sent to the device. (E.g. The device might be offline and will receive it later, but the message was successfully processed).

"canonical_ids":0 Does not mean that there was an error, it means that there were no devices that needed their ID's updated. You can read more about canonical ID's here: http://developer.android.com/google/gcm/adv.html#canonical

On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.

If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.

I would suggest adding some logging code to your client, to make sure that you are not receiving the message. Specifically in your GCMIntentService classes onMessage() method.

这篇关于我从 GCM 服务器收到了这个响应 {“success":1} 但通知没有到达设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆