Asp.net 中的 GCM 请求
GCM request in Asp.net
我已将此 link 提交给发送 GCM 请求,它运行良好。 gcm-push-notification-with-asp-net
我又向 link 推荐了一个 post JSON
how-to-post-json-to-the-server
在第二个的基础上link我尝试了下面的代码
var httprequest = (HttpWebRequest)WebRequest.Create("https://gcm-http.googleapis.com/gcm/send");
httprequest.ContentType = "application/json";
httprequest.Method = "POST";
httprequest.Headers.Add(string.Format("Authorization: key={0}", GCM.APIKey));
httprequest.Headers.Add(string.Format("Sender: id={0}", GCM.ProjectNo));
using (var streamWriter = new StreamWriter(httprequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new GCMValues
{
delay_while_idle = false,
priority = "high",
registration_id = regId,
data = new MessagesValues
{
message = message
}
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httprequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
我用过的属性
public class GCMValues
{
public Object data { get; set; }
public bool delay_while_idle { get; set; }
public String priority { get; set; }
public String registration_id { get; set; }
}
public class MessagesValues
{
public String message { get; set; }
public DateTime? time { get; set; }
}
我面临的问题在第 var httpResponse =(HttpWebResponse)httprequest.GetResponse();
行
我收到错误请求的响应。
我哪里出错了,或者可以做些什么来为 GCM
post 请求传递 JSON
格式的值。
提前致谢。
您在 JSON 中需要 registration_ids
(复数)而不是 registration_id
(单数)。它是一个字符串数组,而不是单个字符串值。
如果您选中 GCM documentation,它会提供所有 JSON 选项。
文档在其他地方详细说明了如何使用 to
如果您只有一个令牌可以向其发送通知,如本例所示:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
我用了下面的方法,成功了。
var httprequest = (HttpWebRequest)WebRequest.Create("https://gcm-http.googleapis.com/gcm/send");
httprequest.ContentType = "application/json";
httprequest.Method = "POST";
httprequest.Headers.Add(string.Format("Authorization: key={0}", GCM.APIKey));
httprequest.Headers.Add(string.Format("Sender: id={0}", GCM.ProjectNo));
String[] regid = regId.Split(',');
using (var streamWriter = new StreamWriter(httprequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new GCMValues
{
delay_while_idle = false,
priority = "high",
registration_ids = regid,
data = new MessagesValues
{
message = message
}
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httprequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
if (result.Contains("Error"))
{
return false;
}
}
我用过的属性
public class GCMValues
{
public Object data { get; set; }
public bool delay_while_idle { get; set; }
public String priority { get; set; }
public String[] registration_ids { get; set; }
}
public class MessagesValues
{
public String message { get; set; }
public DateTime? time { get; set; }
}
我已将此 link 提交给发送 GCM 请求,它运行良好。 gcm-push-notification-with-asp-net
我又向 link 推荐了一个 post JSON
how-to-post-json-to-the-server
在第二个的基础上link我尝试了下面的代码
var httprequest = (HttpWebRequest)WebRequest.Create("https://gcm-http.googleapis.com/gcm/send");
httprequest.ContentType = "application/json";
httprequest.Method = "POST";
httprequest.Headers.Add(string.Format("Authorization: key={0}", GCM.APIKey));
httprequest.Headers.Add(string.Format("Sender: id={0}", GCM.ProjectNo));
using (var streamWriter = new StreamWriter(httprequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new GCMValues
{
delay_while_idle = false,
priority = "high",
registration_id = regId,
data = new MessagesValues
{
message = message
}
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httprequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
我用过的属性
public class GCMValues
{
public Object data { get; set; }
public bool delay_while_idle { get; set; }
public String priority { get; set; }
public String registration_id { get; set; }
}
public class MessagesValues
{
public String message { get; set; }
public DateTime? time { get; set; }
}
我面临的问题在第 var httpResponse =(HttpWebResponse)httprequest.GetResponse();
我收到错误请求的响应。
我哪里出错了,或者可以做些什么来为 GCM
post 请求传递 JSON
格式的值。
提前致谢。
您在 JSON 中需要 registration_ids
(复数)而不是 registration_id
(单数)。它是一个字符串数组,而不是单个字符串值。
如果您选中 GCM documentation,它会提供所有 JSON 选项。
文档在其他地方详细说明了如何使用 to
如果您只有一个令牌可以向其发送通知,如本例所示:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
我用了下面的方法,成功了。
var httprequest = (HttpWebRequest)WebRequest.Create("https://gcm-http.googleapis.com/gcm/send");
httprequest.ContentType = "application/json";
httprequest.Method = "POST";
httprequest.Headers.Add(string.Format("Authorization: key={0}", GCM.APIKey));
httprequest.Headers.Add(string.Format("Sender: id={0}", GCM.ProjectNo));
String[] regid = regId.Split(',');
using (var streamWriter = new StreamWriter(httprequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new GCMValues
{
delay_while_idle = false,
priority = "high",
registration_ids = regid,
data = new MessagesValues
{
message = message
}
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httprequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
if (result.Contains("Error"))
{
return false;
}
}
我用过的属性
public class GCMValues
{
public Object data { get; set; }
public bool delay_while_idle { get; set; }
public String priority { get; set; }
public String[] registration_ids { get; set; }
}
public class MessagesValues
{
public String message { get; set; }
public DateTime? time { get; set; }
}