C# 如何在以下上下文中从响应中获取规范ID?
我对GCM完全陌生,我使用以下方法向GCM服务器发送消息C# 如何在以下上下文中从响应中获取规范ID?,c#,.net,push-notification,google-cloud-messaging,C#,.net,Push Notification,Google Cloud Messaging,我对GCM完全陌生,我使用以下方法向GCM服务器发送消息 public string SendMessage(string RegistrationID, string Message, string AuthString) { //-- Create C2DM Web Request Object --// HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("h
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#中达到同样的效果呢?它没有getCanonicalID方法,因为它是我得到的标准web响应。如何找到规范ID并将其从表中删除?请注意。规范Id是已在其他Id下注册的设备的Id。当您没有此类Id时,规范Id元素将为您提供0。但是如果它有一个值,那么响应中的规范\u id元素将显示计数。在您的回复中,这些id将显示为“注册id”。这些将按照请求发送的顺序进行。因此,如果响应字符串的第3和第4个元素具有“registration_id”的值,则表示您在请求中发送的第3和第4个元素id不是正确的。用响应中“注册ID”中提供的ID替换表中的ID。下面是从C代码中获取这些索引和ID的代码
类程序
{
静态void Main(字符串[]参数)
{
列表ID=新列表();
“成功”7,“失败”0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10:\“123\,\”消息\u id\“:\“0:xxx%xxxxx\”},{\“消息\u id\”:\“0:xxx%xxxxx\”}];
RootObject decryptedMessage=new JavaScriptSerializer()。反序列化(json);
int位置=0;
Dictionary canonicalIdsAndIndexes=新字典();
foreach(decryptedMessage.results中的变量项)
{
if(item.registration\u id!=null)
{
添加(位置、项、注册id);
WriteLine(“消息id:{0},注册id:{1}”,item.message\u id,item.registration\u id);
}
位置++;
}
Console.ReadLine();
}
}
公开课成绩
{
公共字符串消息_id{get;set;}
公共字符串注册\u id{get;set;}
}
公共类根对象
{
公共字符串多播_id{get;set;}
公共int成功{get;set;}
公共int失败{get;set;}
公共int规范_id{get;set;}
公共列表结果{get;set;}
}
规范Id是已在其他Id下注册的设备的Id。当您没有此类Id时,规范Id元素将为您提供0。但是如果它有一个值,那么响应中的规范\u id元素将显示计数。在您的回复中,这些id将显示为“注册id”。这些将按照请求发送的顺序进行。因此,如果响应字符串的第3和第4个元素具有“registration_id”的值,则表示您在请求中发送的第3和第4个元素id不是正确的。用响应中“注册ID”中提供的ID替换表中的ID。下面是从C代码中获取这些索引和ID的代码
类程序
{
静态void Main(字符串[]参数)
{
列表ID=新列表();
“成功”7,“失败”0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10:\“123\,\”消息\u id\“:\“0:xxx%xxxxx\”},{\“消息\u id\”:\“0:xxx%xxxxx\”}];
RootObject decryptedMessage=new JavaScriptSerializer()。反序列化(json);
int位置=0;
Dictionary canonicalIdsAndIndexes=新字典();
foreach(decryptedMessage.results中的变量项)
{
if(item.registration\u id!=null)
{
添加(位置、项、注册id);
WriteLine(“消息id:{0},注册id:{1}”,item.message\u id,item.registration\u id);
}
位置++;
}
Console.ReadLine();
}
}
公开课成绩
{
公共字符串消息_id{get;set;}
公共字符串注册\u id{get;set;}
}
公共类根对象
{
公共字符串多播_id{get;set;}
公共int成功{get;set;}
公共int失败{get;set;}
公共int规范_id{get;set;}
公共列表结果{get;set;}
}
class Program
{
static void Main(string[] args)
{
List<String> ids = new List<String>();
String json ="{\"multicast_id\": \"xxxxx\",\"success\": 7,\"failure\": 0,\"canonical_ids\": 2,\"results\": [{\"message_id\": \"0:xxx%xxxxx\"}, {\"message_id\": \"0:xxx%xxxxx\"}, {\"registration_id\": \"456\",\"message_id\": \"0:xxx%xxxxx\"}, {\"message_id\": \"0:xxx%xxxxx\"}, {\"message_id\": \"0:xxx%xxxxx\"}, {\"registration_id\": \"123\",\"message_id\": \"0:xxx%xxxxx\"}, {\"message_id\": \"0:xxx%xxxxx\"}]}";
RootObject decryptedMessage = new JavaScriptSerializer().Deserialize<RootObject>(json);
int position = 0;
Dictionary<int, String> canonicalIdsAndIndexes = new Dictionary<int, string>();
foreach (var item in decryptedMessage.results)
{
if (item.registration_id != null)
{
canonicalIdsAndIndexes.Add(position, item.registration_id);
Console.WriteLine("message id: {0}, registrationid: {1}", item.message_id, item.registration_id);
}
position++;
}
Console.ReadLine();
}
}
public class Result
{
public string message_id { get; set; }
public string registration_id { get; set; }
}
public class RootObject
{
public string multicast_id { get; set; }
public int success { get; set; }
public int failure { get; set; }
public int canonical_ids { get; set; }
public List<Result> results { get; set; }
}