我从GCM服务器获得此响应{“成功”:1}但通知未到达设备
我试图通过推送通知通知设备
我收到了来自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);
提前致谢
请查看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()方法中。
- 图像使用c#web服务从Android应用程序上传到服务器
- Xamarin Resource.designer.cs没有为新添加文件生成引用
- 如何使用Unity3D将AndroidJavaObject中的变量转换为C#类
- 无法使用Appium和C#在Android模拟器的浏览器中找到元素
- 适用于Ubuntu的Android for MonoDevelop
- 由于服务器执行失败,Visual Studio模拟器无法启动
- Android:如何将ExecuteScalar移植到Java?
- Android相当于Windows Phone的Deployment.Current.Dispatcher.BeginInvoke?
- 如何使用Unity3D获取已安装的应用列表?