Server for GCM push notifications for android in C #

I am working on an android application that uses push notification service. I'm currently stuck on creating a server. The guides provided by the GCM documentation are presented in java, which I have no idea how to implement. After some research, I discovered GCMSharp on github that uses C #

PushSharp - https://github.com/Redth/PushSharp

But at the moment I'm new to creating a server and don’t know how to get started. Is the server actually a web service that continues to listen to the request and, after receiving the request, sends it to GCM, which pushes the notification to the client phone?

And if so, am I implementing it in a web service like WCF?

+4
source share
2 answers

You can complete this tutorial.

Is the server actually a web service that continues to listen to the request? and, upon receiving the request, sends it to GCM, which pushes the notification to the client phone?

You do not need to listen to requests. GCM Push directly transmits any message to the device without any request. See this documentation for more details .

+5
source

, . , . push- 2 , #. , . . -, .

: hardcorded apiKey, deviceId postData, , apiKey, deviceId postData .

public string SendGCMNotification(string apiKey, string deviceId, string postData)
{
    string postDataContentType = "application/json";
    apiKey = "AIzaSyC13...PhtPvBj1Blihv_J4"; // hardcorded
    deviceId = "da5azdfZ0hc:APA91bGM...t8uH"; // hardcorded

    string message = "Your text";
    string tickerText = "example test GCM";
    string contentTitle = "content title GCM";
    postData =
    "{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
      "\"data\": {\"tickerText\":\"" + tickerText + "\", " +
                 "\"contentTitle\":\"" + contentTitle + "\", " +
                 "\"message\": \"" + message + "\"}}";


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

, , apiKey, deviceId. , , .

apiKey
: , GCM.
: Refer

deviceId
: RegistrationId. . .
create: , . pushNotification Plugin. deviceId/RegistrationId, . . Google, senderId, =)

- , .

.
-Charitha -

+1

All Articles