2

我使用 GCM 创建了一个适用于 Android 的推送通知。
该服务面临许多超时,并且间歇性地工作。我有一个 Windows 服务,它在任意时间(在这种情况下为 30 秒)运行并检查 Message Queue ,如果存在任何消息,它会将其发送到相应的设备。
模式是:
它工作前两次三次,如果一次失败,它开始连续失败。

失败的原因可能是什么?

编辑 我认为这可能是由于组织的防火墙而发生的,但由于我在开放网络上尝试过,这也可以被推翻。 任何输入任何思路都会有所帮助。 提前致谢

代码 :

private void SendAndroidMessage(string deviceID,string Message ,DeviceDataModel ddm)
        {

         StringBuilder postMsgBuilder = new StringBuilder();
            postMsgBuilder.Append("registration_id=").Append(deviceID);
            //postMsgBuilder.Append("&").Append("delay_while_idle=").Append("false");
            postMsgBuilder.Append("&").Append("data.message=").Append(Message);

            byte[] messageBytes = Encoding.UTF8.GetBytes(postMsgBuilder.ToString());

            var request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");

            // Hook a callback to verify the remote certificate
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(C2dmCertValidationCallback);
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            request.ContentLength = messageBytes.Length;
            request.Headers.Add(HttpRequestHeader.Authorization, "key=" + AuthorisationKey);

            bool sent = false;
            int tries = 0;
            HttpWebResponse response;
            while (!sent && tries < this.numOfRetries)
            {
                try
                {
                    using (var requestStream = request.GetRequestStream())
                    {
                        // we are not using retries yet.. or looking for errors here. We should log errors too.
                        requestStream.Write(messageBytes, 0, messageBytes.Length);
                    }

                    // Sends the notification and gets the response.
                    response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return;
                    }

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        tries++;
                        if (tries > this.numOfRetries)
                        {
                            if (this.NotificationFailed != null)
                            {
                                this.NotificationFailed(this, new NotificationEventArgs(new NotificationException(string.Format(CultureInfo.InvariantCulture, "{0} notification failures for {1} for DeviceId:{2} of DeviceType:{3}. Giving up", this.numOfRetries, C2dmUrlStr,ddm.DeviceId,ddm.DeviceType))));
                            }

                            return;
                        }
                    }
                    else
                    {
                        sent = true;
                    }
                }
                catch (WebException e)
                {
                    // check why we got the exception 
                    string responseString = "";
                    if (e.Response != null)
                    {
                        StreamReader reader = new StreamReader(e.Response.GetResponseStream());
                       responseString= reader.ReadToEnd();
                    }
                    if (this.NotificationFailed != null)
                    {
                        this.NotificationFailed(this, new NotificationEventArgs(new NotificationException("Notification failed with exception " + e.Message + "for " + "https://android.googleapis.com/gcm/send"+" DeviceId: "+ddm.DeviceId+" Device Type: "+ddm.DeviceType+"Response String : "+ responseString, e)));
                    }

                    throw;
                }
            }
        }
4

1 回答 1

2

我曾经也遇到过类似的问题,奇怪的是,这是因为我在读取响应流后没有关闭它。在您response.Close();使用response. 我希望它也适合你。

于 2012-08-03T06:55:41.217 回答