티스토리 뷰

namespace GooglePush
{
    public class AndroidGCMPushNotification
    {
      
        public const string GOOGLE_GCM_SERVER = "https://android.googleapis.com/gcm/send";

        private string googleAppId;
        private string senderId;

        
        public string GoogleAppId
        {
            get { return this.googleAppId; }
            set { this.googleAppId = value; }
        }
        public string SenderId
        {
            get { return this.senderId; }
            set { this.senderId = value; }
        }

        public AndroidGCMPushNotification(string googleAppId, string senderId)
        {
            this.googleAppId = googleAppId;
            this.senderId = senderId;
        }
 
        public string SendNotification(string deviceId, string message)
        {
            WebRequest webRequest = WebRequest.Create(GOOGLE_GCM_SERVER);
            webRequest.Method = "post";
            webRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            webRequest.Headers.Add(string.Format("Authorization: key={0}", this.googleAppId));
            webRequest.Headers.Add(string.Format("Sender: id={0}", this.senderId));

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

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

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

            WebResponse webResponse = webRequest.GetResponse();
            dataStream = webResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(dataStream);

            String sResponseFromServer = streamReader.ReadToEnd();

            streamReader.Close();
            dataStream.Close();
            webResponse.Close();

            return sResponseFromServer;
        }
    }
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함