Push Notification (GCM) Using Asp.net (the notific

2019-09-07 01:47发布

问题:

I've Create An Application in Google Cloud Messaging (GCM) And they Give Me :

  1. SENDER-ID
  2. API-KEY

I've Create An Android Application that Clients Can Used To Register their Devices
to the cloud .. (And It's OK).

Now I want to Push A notification to Rest Of Devices if Any User Using My Android Application Change Something in the data (SQL SERVER DATABASE) .

I found This Code ...

 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;
         }

But When I Need To Execute the Method (It Ask Me to give it a parameter of 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 + "\"}}";

First Of All --> Does This Method Will Helps me to send a push notification correctly ?? or it can be improved to be better ?

if it good ...

from where can i get the Browser KEY ??

Thanks In Advance :)

回答1:

You have to obtain your Browser-Key from Google APIs Console. https://code.google.com/apis/console

Your Browserkey will be 40 bytes in size.

You can refer http://developer.android.com/google/gcm/gs.html for further help.