我正在努力从我的asp.net项目向Android手机发送推送消息 我已经把register_id放在我的代码中,但我处理错误登记错误,我不明白它的原因。
这是我的代码:
public void SendMessage(string registrationId, string data, string sAuth)
{
string collapseKey = Guid.NewGuid().ToString("n");
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
string url = "https://android.apis.google.com/c2dm/send?";
url = url + HttpUtility.UrlEncode("registration_id=" + registrationId + "&collapse_key=" + collapseKey + "&data.payload=" + data);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "GoogleLogin auth=" + sAuth);
//request.ContentLength = 0;
string encoded = HttpUtility.UrlEncode(url);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] buffer = encoding.GetBytes(encoded);
Stream newStream = request.GetRequestStream();
newStream.Write(buffer, 0, buffer.Length);
newStream.Close();
//Reading return Response
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream);
string value = sr.ReadToEnd().ToString();
Label1.Text = value;
//Response.Write(sr.ReadToEnd());
sr.Close();
resStream.Close();
}
}
catch (Exception ex)
{
throw (ex);
}
}
答案 0 :(得分:1)
可以试试这段代码。我认为问题来自网址和请求参数。
public void SendMessage(string registrationId, string data, string sAuth)
{
string collapseKey = Guid.NewGuid().ToString("n");
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
string url = "https://android.apis.google.com/c2dm/send";
string params = HttpUtility.UrlEncode("registration_id=" + registrationId + "&collapse_key=" + collapseKey + "&data.payload=" + data);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "GoogleLogin auth=" + sAuth);
//request.ContentLength = 0;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] buffer = encoding.GetBytes(params);
Stream newStream = request.GetRequestStream();
newStream.Write(buffer, 0, buffer.Length);
newStream.Close();
//Reading return Response
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream);
string value = sr.ReadToEnd().ToString();
Label1.Text = value;
//Response.Write(sr.ReadToEnd());
sr.Close();
resStream.Close();
}
}
catch (Exception ex)
{
throw (ex);
}
}
答案 1 :(得分:0)
如果它是第一次在您的设备上运行您的应用程序,那么您必须处理首次注册ID。更多细节可以在这里找到:http://code.google.com/android/c2dm/index.html
如果不是这样,那么您必须考虑这个问题:“C2DM服务器会定期刷新注册ID。” Witch意味着您必须处理每次刷新注册ID。
希望它有所帮助。
答案 2 :(得分:0)
solve解决这样的问题;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://android.clients.google.com/c2dm/send");
request.Method = "POST";
request.KeepAlive = false;
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);
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=" + authTokenString);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//savas
ServicePointManager.ServerCertificateValidationCallback += delegate(
object
sender,
System.Security.Cryptography.X509Certificates.X509Certificate
pCertificate,
System.Security.Cryptography.X509Certificates.X509Chain pChain,
System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
{
return true;
};
////***********
WebResponse response = request.GetResponse();
HttpStatusCode responseCode = ((HttpWebResponse)response).StatusCode;
if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
{
Console.WriteLine("Unauthorized - need new token");
}
else if (!responseCode.Equals(HttpStatusCode.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();
答案 3 :(得分:0)
在此方法中SendMessage(string registrationId, string data, string sAuth)
未在 Andriod 设备中收到通知,并且没有任何error
消息,该怎么办?