我在Google云消息传递(GCM)中创建了一个应用程序 他们给我:
我创建了一个客户端可以用来注册设备的Android应用程序 到了云..(并且没关系)。
现在,如果使用我的Android应用程序的任何用户更改了数据中的某些内容(SQL SERVER DATABASE),我想向其他设备发送通知。
我找到了此代码......
private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
{
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
//
// MESSAGE CONTENT
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//
// CREATE REQUEST
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
Request.Method = "POST";
Request.KeepAlive = false;
Request.ContentType = postDataContentType;
Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.ContentLength = byteArray.Length;
Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//
// SEND MESSAGE
try
{
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
var text = "Unauthorized - need new token";
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
var text = "Response from web service isn't OK";
}
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadToEnd();
Reader.Close();
return responseLine;
}
catch (Exception e)
{
}
return "error";
}
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
但当我需要执行方法时(它要求我给它一个参数Browser-APIKey
)
string deviceId = "APA91bHsQUsnYLHSFkmmJE8AgXEU--_nqPOJ5q2sfZIpCI1ZiJnmi2-IrZCqwummfJB94uVmqgT-ZWkyeIrICU8GpPvAOdmUfiVtYRmmA7bVAaKPuerJUcRUisveOe5Jp36-3fUK7VlDvwcme0SaJiwJU9B1y1EkF6YTQ00g";
string message = "some test message";
string tickerText = "example test GCM";
string contentTitle = "content title GCM";
string postData =
"{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
"\"data\": {\"tickerText\":\"" + tickerText + "\", " +
"\"contentTitle\":\"" + contentTitle + "\", " +
"\"message\": \"" + message + "\"}}";
首先 - >这种方法会帮我正确发送推送通知吗?或者它可以改进得更好?
如果它好......
从哪里可以获得Browser KEY
??
提前致谢:)
答案 0 :(得分:1)
您必须从Google API控制台获取浏览器密钥。 https://code.google.com/apis/console
您的Browserkey大小为40个字节。
您可以参考http://developer.android.com/google/gcm/gs.html获取进一步的帮助。