Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I posted notifications from project(asp mvc) with gcm.

I receive the following error when sending notifications.

and am checked the api key and sender id both are same.

error System.Net.WebException: The remote server returned an error: (401) Unauthorized.

please guide me.

public Notification PushToAndroidDevice(string registrationid, string message)
{
Notification notification = new Notification();
//try
//{
//var applicationID = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs";//Server Key
var applicationID = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxxs";//Browser Key
var SENDER_ID = "2xxxxxxxxxxxxxxxxxxxx5";

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(HttpRequestHeader.Authorization, "key=AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs");

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;
}
Posted

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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