JSON way
user1551788 answer works fine, however I like to do it in JSON, which is better practice instead of inserting everything in one line, I think.
The internal class 'jsonObj' is the same as the documentation requires, check the different requests you can make here
A brief description:
to: the phone to send to, insert the registrationId that you received from the phone here.
delay_while_idle By using the delay_while_idle flag, notifications will be delivered once the device becomes active. (out of lock, when the user really uses the phone).
data: set data
with custom key/value pairs to pass additional payload to the client app. So, you can put in any variable you want, if you like a json string that contains another object, as long as it does not exceed 4 KB.
Some that are also available that I didn't use.
collapse_key: If set, a notification that has the same collapse_key
name should be overwrite the older notification (metter of good implementation at the phone side when notification is send, on the GCM server it will overwrite when the notification is pending).
time_to_live: Straight forward, how long the notification will stay alive, currently not supported for IOS.
Some other, see documentation.
internal class because I didn't need that object outside of my class, which is better for naming like 'data' that could be anything.
private void SendPostsToGCM(jsonObj jsonObj)
{
string senderId = "your project number (google)";
string apiKey = "your apiKey, found on console";
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
tRequest.Headers.Add(string.Format("Authorization: key={0}", apiKey));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
string jsonPostData = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj); //download Newtonsoft NuGet package
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonPostData);
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();
string response = sResponseFromServer;
tReader.Close();
dataStream.Close();
tResponse.Close();
}
internal class jsonObj
{
public bool delay_while_idle { get; set; }
public data data { get; set; }
public string to { get; set; }
}
internal class data
{
public int Id { get; set; }
public string text { get; set; }
}
To use, simply:
//some filtering to select some posts or whatever.
jsonObj jsonPostData = new jsonObj()
{
delay_while_idle = true,
to = registrationGCMid,
data = new data()
{
Id = post.id,
text = post.text,
}
};
SendPostsToGCM(jsonPostData);
Another great difference I have noticed, the google service returns a json string containing useful information, it tells how many succeeded and how many failed, etc.