我正在使用c2dm-sharp
向Android设备发送推送通知。
我工作正常,但有一段时间它出现了一些错误:message failed invalid registration
当我调试代码时,我在C2dmMessageTransport.cs
项目的C2dmSharp.Server
项目中找到了var webResp = webReq.GetResponse() as HttpWebResponse;
错误是 - the underlying connection was closed an unexpected error occurred on a send
static C2dmMessageTransportResponse send(C2dmMessage msg, string googleLoginAuthorizationToken, string senderID, string applicationID)
{
C2dmMessageTransportResponse result = new C2dmMessageTransportResponse();
result.Message = msg;
var postData = msg.GetPostData();
var webReq = (HttpWebRequest)WebRequest.Create(C2DM_SEND_URL);
// webReq.ContentLength = postData.Length;
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.UserAgent = "C2DM-Sharp (version: 1.0)";
webReq.Headers.Add("Authorization: GoogleLogin auth=" + googleLoginAuthorizationToken);
using (var webReqStream = new StreamWriter(webReq.GetRequestStream(), Encoding.ASCII))
{
var data = msg.GetPostData();
webReqStream.Write(data);
webReqStream.Close();
}
try
{
var webResp = webReq.GetResponse() as HttpWebResponse;
if (webResp != null)
{
result.ResponseStatus = MessageTransportResponseStatus.Ok;
//Check for an updated auth token and store it here if necessary
var updateClientAuth = webResp.GetResponseHeader("Update-Client-Auth");
if (!string.IsNullOrEmpty(updateClientAuth) && C2dmMessageTransport.UpdateGoogleClientAuthToken != null)
UpdateGoogleClientAuthToken(updateClientAuth);
//Get the response body
var responseBody = "Error=";
try { responseBody = (new StreamReader(webResp.GetResponseStream())).ReadToEnd(); }
catch { }
//Handle the type of error
if (responseBody.StartsWith("Error="))
{
var wrErr = responseBody.Substring(responseBody.IndexOf("Error=") + 6);
switch (wrErr.ToLower().Trim())
{
case "quotaexceeded":
result.ResponseStatus = MessageTransportResponseStatus.QuotaExceeded;
break;
case "devicequotaexceeded":
result.ResponseStatus = MessageTransportResponseStatus.DeviceQuotaExceeded;
break;
case "invalidregistration":
result.ResponseStatus = MessageTransportResponseStatus.InvalidRegistration;
break;
case "notregistered":
result.ResponseStatus = MessageTransportResponseStatus.NotRegistered;
break;
case "messagetoobig":
result.ResponseStatus = MessageTransportResponseStatus.MessageTooBig;
break;
case "missingcollapsekey":
result.ResponseStatus = MessageTransportResponseStatus.MissingCollapseKey;
break;
default:
result.ResponseStatus = MessageTransportResponseStatus.Error;
break;
}
throw new MessageTransportException(wrErr, result);
}
else
{
//Get the message ID
if (responseBody.StartsWith("id="))
result.MessageId = responseBody.Substring(3).Trim();
}
}
}
catch (WebException webEx)
{
var webResp = webEx.Response as HttpWebResponse;
if (webResp != null)
{
if (webResp.StatusCode == HttpStatusCode.Unauthorized)
{
//401 bad auth token
result.ResponseCode = MessageTransportResponseCode.InvalidAuthToken;
result.ResponseStatus = MessageTransportResponseStatus.Error;
throw new InvalidAuthenticationTokenTransportException(result);
}
else if (webResp.StatusCode == HttpStatusCode.ServiceUnavailable)
{
//First try grabbing the retry-after header and parsing it.
TimeSpan retryAfter = new TimeSpan(0, 0, 120);
var wrRetryAfter = webResp.GetResponseHeader("Retry-After");
if (!string.IsNullOrEmpty(wrRetryAfter))
{
DateTime wrRetryAfterDate = DateTime.UtcNow;
if (DateTime.TryParse(wrRetryAfter, out wrRetryAfterDate))
retryAfter = wrRetryAfterDate - DateTime.UtcNow;
else
{
int wrRetryAfterSeconds = 120;
if (int.TryParse(wrRetryAfter, out wrRetryAfterSeconds))
retryAfter = new TimeSpan(0, 0, wrRetryAfterSeconds);
}
}
//503 exponential backoff, get retry-after header
result.ResponseCode = MessageTransportResponseCode.ServiceUnavailable;
result.ResponseStatus = MessageTransportResponseStatus.Error;
throw new ServiceUnavailableTransportException(retryAfter, result);
}
}
}
return result;
}
我被困在这里请帮助我。 是我缺少的任何东西
答案 0 :(得分:0)
您是否已注册C2DM服务并将您的个人密钥输入? http://code.google.com/android/c2dm/signup.html
您收到的代码记录在此处: http://code.google.com/android/c2dm/index.html#push
P.S。我从这里获得了这两个链接:https://github.com/Redth/C2DM-Sharp在如何使用它?和链接部分下。