我正在 C# 中创建一个类,该类具有使用 GCM 在 android 中发送推送通知的方法。该方法运行良好,并且谷歌也给出了成功的响应。但在 android 模拟器中,通知为空。这是我正在使用的代码,
public void NotifyTest(string regId)
{
var applicationID = "AIza*************";
var SENDER_ID = "xxxxxxxxxx";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
httpWebRequest.Headers.Add(string.Format("Sender: key={0}", SENDER_ID));
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"registration_ids\":[\""+ regId +"\"]," +
"\"data\": { \"score\" : \"1234\"}}";
Console.WriteLine(json);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
}
}
该代码工作正常,没有任何错误,并且也返回了 Google 的响应。请让我知道这些建议。