Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[WebMethod(Description = "Google Messages")]
public string SendGoogleMessagesToDevice(string message, string googleAppId, string registrationId)
{

message = string.Concat(message);
//Debugger.Launch();
//SendNotification(message, googleAppId, registrationId);
System.Net.WebRequest tRequest;
// Take it form web.config
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
// plane text processing other than json
tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";//"application/json";//
tRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + googleAppId);

string postData = "data.message=" + message + "®istration_id=" + registrationId + "";



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;

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900