일단 GCM Service 를 등록하고, 앱에서 폰의 ID 를 생성했다면, 이제 Push 서버에서 그 ID 를 이용해 메시지를 보내면 된다.
일반적으로넌 앱이 실행 될때 관리 서버로 생성한 ID 를 전달해 주지만, 여기서는 어차피 혼자서 쓰는 것이니, 이미 전달되어 있다고 생각하고...
Push 서버에서 메시지를 보내보자.
거창하게 Push 서버라고 말은 하지만, 그냥 구글 GCM Service 에 등록 ID 와 메시지를 던져 주는 역할을 할 뿐이다.
그냥 아무 웹페이지나 하나 만들고...
소스를 이렇게 기록한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;
public partial class gcmtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
SendNotification("전달 받은 앱의 등록 키를 여기에 기록", "GCM TEST를 합니다.");
}
public string SendNotification(string regId, string message)
{
string GoogleAppID = "API Key 를 기록";
var SENDER_ID = "프로젝트 넘버";
var value = HttpUtility.UrlEncode(message);
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.title=test&data.msg="
+ value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + regId + "";
Console.WriteLine(postData);
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();
return sResponseFromServer;
}
}
여기서 GoogleAppID 는 구글에서 등록한 서비스 Key 이며.
SENDER_ID 는 프로젝트 넘버.
regId 는 폰에서 앱이 실행 할때 생성한 ID 이다.
이렇게 하고...
저 버튼을 클릭하면...
이렇게 폰으로 메시지가 전달이 된다.
여기에 나온는 메시지는..
웹에서 저 파라메터로 넘겨지고...
앱에서...
저기서 처리되니... 적당히 알아서 쓰면되겠다.






