I am using Asp.net MVC 4 Web API as a third party server to Push Notification for Android Device using GCM. It's working fine notification are being generated but the message is blank. I have spent whole day on it but could not find any solution please help me
Web API Function to Send Push Notification is as follows:
public Notification PushToAndroidDevice(string registrationid,string message)
{
Notification notification = new Notification();
try
{
var applicationID = "MY_APPLICATION_ID";
var SENDER_ID = "MY_SENDER_ID";
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/x-www-form-urlencoded";
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
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.message=" + message + "&data.time=" + System.DateTime.Now.ToString() + "®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();
notification.Message = sResponseFromServer;
tReader.Close();
dataStream.Close();
tResponse.Close();
notification.Status = true;
}
catch (Exception ex)
{
notification.Status = false;
notification.Message = "ERROR DESCRIPTION : " + ex.Message;
}
return notification;
}
Notification is a class having two properties Status bool and Message string
public class Notification
{
public bool Status { get; set; }
public string Message { get; set; }
}
By this code I'm able to send notification message on android mobile but the notification is blank please help me out ...........