Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have successful to register device id on gcm server from android app.
Now i want send notification from C# winform application.I am using this following code but i am getting error called
The remote server returned an error: (401) Unauthorized.

Please help me
This is my code

What I have tried:

string GoogleAppID = "AIzaSyB8vlCeU_MIiDVXFYl0uOxZNt9xZF7jHQ4";
var SENDER_ID = "851345542956";
var value = 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.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
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;
Posted
Comments
[no name] 28-Sep-16 12:20pm    
"Unauthorized" is a pretty clear error message and usually means the same no matter the context. What did the google api support people say when you asked them?

1 solution

I have used the PushSharp library with success. We were sending push notifications from a windows service to phones. Link https://github.com/Redth/PushSharpPushSharp
 
Share this answer
 
Comments
Timo Tielens 4-Oct-16 2:59am    
Please check your link.

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



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