블로그 이미지
ballrok

카테고리

분류 전체보기 (5)
Programming (3)
Photo (2)
Server (0)
Total39,707
Today12
Yesterday17

달력

« » 2013.02
          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    

공지사항

태그목록

'2013/02/14'에 해당되는 글 1건

  1. 2013.02.14 Android GCM 전송을 하는 웹서비스.

GCM 테스트를 하기 위해서 웹 서비스를 만들어 봤음.

전송할 단말기 키값, 구글 api key, api id, 전달 메시지를 인자로 받아서

해당 단말기에 전송이 가능하다.

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Threading;
using System.Web;

namespace gcm_sender
{
    public class Service1 : gcm_sender
    {
        public bool onSendGCM(string deviceID, string google_api_key, string google_api_id, string message)
        {
            bool result = false;

            AndroidGCMPushNotification noti = new AndroidGCMPushNotification(google_api_key, google_api_id);
            string response = noti.SendNotification(deviceID, message);

            if (response.Contains("id=0")) result = true;
            else result = false;

            return result;
        }
    
    }
    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=1";
            postData += "&delay_while_idle=1";
            postData += "&data.message=" + System.Web.HttpUtility.UrlEncode(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;
        }
    }
}

Posted by ballrok

최근에 달린 댓글

최근에 받은 트랙백

글 보관함