0

我在 Google Cloud Messaging (GCM) 中创建了一个应用程序,他们给了我:

  1. 发件人ID
  2. API-KEY

我已经创建了一个 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??

提前致谢 :)

4

1 回答 1

1

您必须从 Google API 控制台获取您的浏览器密钥。https://code.google.com/apis/console

您的 Browserkey 大小为 40 字节。

您可以参考http://developer.android.com/google/gcm/gs.html以获得更多帮助。

于 2013-01-07T09:38:30.347 回答