我正在使用 C# 为 Android 实现 GCM 推送通知服务器端:问题是设备没有收到通知,我不知道问题出在哪里,是来自服务器还是来自应用程序?这是服务器端响应“id=0:1373508394613753%f73a6d8d66d6cf16”,我不知道这个响应是什么意思?所以任何人都可以告诉我这个响应是否意味着通知已发送但设备有问题,反之亦然?
这是服务器端代码:
public string SendAndroidPushNotification(string RegistrationID, string message)
{
string GoogleAppID = "AIzaSyAq8TKNRJpp18B9Xvc6PcM4ji8igU";
var SENDER_ID = "5525610314545";
var value = message;
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + RegistrationID + "";
//Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
HttpWebResponse httpResponse = (HttpWebResponse)tResponse;
string statusCode = httpResponse.StatusCode.ToString();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
这是对 Andriod 应用程序代码的参考: