繁体   English   中英

使用Asp.net推送通知(GCM)(未收到通知)(浏览器密钥)

[英]Push Notification (GCM) Using Asp.net (the notification wasn't received) (Browser Key)

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

  1. 发件人ID
  2. API密钥

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

提前致谢 :)

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

您的Browserkey大小为40个字节。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM