Tuesday, 15 December 2015

Push notification for android using C#.Net

            // App ID
            string googleAppID = "AIzaSyAYfVEk_C0ATkqbZo6mp2JI1c8bv58wV54";
            // sender key which is generated from Google
            var senderID = "649193597288";
            // Device ID which is used to pull message from android server
            string deviceID = "APA91bFJGvf3zyfVxuTu6WgZKnrSMch6OXUGMqZtean2q9lQ7k-5cibteOgh3wnxME7ex…… ";
            var message = "This is test push notification for android";
           
            WebRequest request;
            request = WebRequest.Create("https://android.googleapis.com/gcm/send");
            request.Method = "post";
            request.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
            request.Headers.Add(string.Format("Authorization: key={0}", googleAppID));

            request.Headers.Add(string.Format("Sender: id={0}", senderID));

            string postData = @"collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" +
            System.DateTime.Now.ToString() + "&registration_id=" + deviceID + "";

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            String sResponseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

No comments:

Post a Comment