我这有响应从GCM服务器{"成功":1}但通知不来的设备不来、这有、服务器、通知

2023-09-05 08:44:42 作者:别回头~

我想通过推送通知来通知设备

I am trying to Notify the Device by the Push Notification

我收到这个响应从 GCM 服务器

I Received this Response From THE GCM Server

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

但仍然没有得到通知

使用知识 - > 成功:1

但我认为有一些错在这里 - > canonical_ids:0

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

这是我的code ...

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

在此先感谢

推荐答案

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

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.

我理解的意思是,GCM能够处理该消息,但这并不意味着该邮件成功发送到设备。 (例如,设备可能处于离线状态,稍后会接受它,但消息被成功处理)。

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不是说有一个错误,就意味着没有任何设备在需要它们的ID的更新。你可以阅读更多有关规范ID是在这里: http://developer.android.com/谷歌/ GCM / adv.html#规范

"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提供了一个名为规范注册的ID来轻易设施   从这些情况中恢复。一个规范的注册ID被定义   为您的应用程序所要求的最后注册的ID。   这是将消息发送到服务器时应该使用的ID   设备。

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.

如果您稍后尝试发送使用不同的注册信息   的ID,GCM将处理该请求和往常一样,但会包括   在的的registration_id领域的规范的注册ID   响应。请务必将存储在注册ID您   服务器与此规范的标识,作为最后的ID您使用的是意志   停止工作。

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.

我会建议增加一些记录code到客户端,以确保您没有收到该消息。特别是在你的GCMIntentService类onMessage()方法。

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.