Android sending push notification through .net webservices
Asked Answered
R

1

17

I am using following webservice for sending Push notification from Android. When I call this webservice first time, it takes so much time and push notification is not delivered on Android Device. It happens only when called from Android. It works perfectly as webservice.

[WebMethod]

    public string SendGcm(String serviceKey,String registrationId ,string message) {
        WebClient wc=new WebClient();
        wc.Headers.Add("Authorization", "key=" + serviceKey);
        NameValueCollection nameValues=new NameValueCollection
            {
                {"registration_id", registrationId},
                {"collapse_key", Guid.NewGuid().ToString()},
                {"data.payload", message}
            };

        var resp=wc.UploadValues("https://android.googleapis.com/gcm/send",
                    nameValues);

        var respMessage = Encoding.Default.GetString(resp);
                return respMessage;
    }
Roussillon answered 28/11, 2012 at 12:3 Comment(2)
Ideally it should not, but can u check if your code works with a test echo server instead of google server as well.Avigdor
Probably due to Throttling developer.android.com/google/gcm/adv.html#throttlingGynaeco
D
0

Use this - :

 public void MakeNotificationForAndroid(string DeviceToken, string Body, string Sound, string CustomFrom, string CustomeMsg)
        {
            String DeviceID = "";

            DeviceID = DeviceToken;
            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/x-www-form-urlencoded";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", "AIzaSyBX1gD47uiVp0W_UjNxhwtVsQCNJYfg5vI"));

            String collaspeKey = Guid.NewGuid().ToString("n");
            //String postData=string.Format("registration_id={0}&data.payload={1}&collapse_key={2}", DeviceID, "Pickup Message", collaspeKey);
            String postData = string.Format("registration_id={0}&data.message={1}&collapse_key={2}&data.sound={3}&data.type={4}", DeviceID, Body, collaspeKey, Sound, CustomeMsg);

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            tReader.Close();
            dataStream.Close();
            tResponse.Close();
        }
Dianadiandra answered 19/9, 2013 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.