我是GCM的新手,我使用以下方法将消息发送到GCM服务器。
public string SendMessage(string RegistrationID, string Message, string AuthString)
{
//-- Create C2DM Web Request Object --//
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.clients.google.com/c2dm/send");
Request.Method = "POST";
Request.KeepAlive = false;
//-- Create Query String --//
NameValueCollection postFieldNameValue = new NameValueCollection();
postFieldNameValue.Add("registration_id", RegistrationID);
postFieldNameValue.Add("collapse_key", "1");
postFieldNameValue.Add("delay_while_idle", "0");
// postFieldNameValue.Add("data.message", Message);
postFieldNameValue.Add("data.payload", Message);
string postData = GetPostStringFrom(postFieldNameValue);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
Request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
Request.ContentLength = byteArray.Length;
Request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthString);
//-- Delegate Modeling to Validate Server Certificate --//
ServicePointManager.ServerCertificateValidationCallback += delegate(
object
sender,
System.Security.Cryptography.X509Certificates.X509Certificate
pCertificate,
System.Security.Cryptography.X509Certificates.X509Chain pChain,
System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
{
return true;
};
//-- Create Stream to Write Byte Array --//
Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//-- Post a Message --//
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
return "Unauthorized - need new token";
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
return "Response from web service isn't OK";
//Console.WriteLine("Response from web service not OK :");
//Console.WriteLine(((HttpWebResponse)Response).StatusDescription);
}
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadLine();
Reader.Close();
return responseLine;
}
我需要在这种情况下处理规范ID。我看到了下面的代码片段,可以在Java中使用。
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister database
}
}
但是我怎样才能在C#中实现同样的目标?它没有getCanonicalIds方法,因为它是我得到的标准Web响应。如何找到规范ID并将其从表中删除?请指教。
答案 0 :(得分:0)
规范ID是已经在不同Id下注册的设备ID。当你没有这样的Id时,canonical_ids元素会给你0但是如果它有一个值,那么响应中的canonical_ids元素将显示计数。在您的回复中,这些ID将表示为" registration_id"。这些将按请求发送的顺序排列。因此,如果响应字符串的第3和第4个元素具有" registration_ids"的值。这意味着您在请求中发送的第3个和第4个元素ID不正确。使用" registration_ids"中提供的ID替换它们。在回应中。下面是从C#代码中获取这些索引和ID的代码。
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
<handlers>
<add
name="DownFile"
path="/api/downloads/MyDownload.axd"
type="MyProject.WebApi.Handlers.MyDownloadAsyncHandler, MyProject.WebApi"
verb="GET"/>
</handlers>
</system.webServer>