我已在GCM中注册了我的设备,但我的推送GCM无法正常工作。我的代码出了什么问题?
asp.net方
protected void Button1_Click(object sender, EventArgs e)
{
send("APA91bHwwSLPkdIfqxW8gsosp1J1zgUUsHsgY1LFxetxig4Abo3MfYg7mdbvU3gqLMN5VhW08HvHKhxsHzRAqbRs7WtE6jAQux77XZpx9_1p20O9LMrzpa9mw_RmXoyfq5bkxsLEHO7n6ZGWHzmfzZxaimp46GwR8g");
}
void send(string regId)
{
try
{
var applicationID = "tokyo-***se-***12";
var SENDER_ID = "8500****7538";
var value = "push 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}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
// string postData = "{ 'registration_id': [ '" + regId + "' ], 'data': {'message': '" + txtMsg.Text + "'}}";
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=" + regId + "";
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();
lblStat.Text = sResponseFromServer;
tReader.Close();
dataStream.Close();
tResponse.Close();
}
catch (Exception w)
{
Response.Write(w.Data);
}
}
的web.config
<appSettings >
<add key="GOOGLE_API_KEY" value="AIzaSyCuXwVivtKDKBqS2zxGCrD7Cc2Qwpjxgcw"/>
</appSettings>
答案 0 :(得分:0)
看起来您在这里发送了错误的授权密钥:
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
您需要将applicationID
更改为您分配给GOOGLE_API_KEY
的API密钥。
最后,您可能需要考虑从此StackOverflow帖子中隐藏您的实际API密钥。