【发布时间】:2016-08-10 12:31:40
【问题描述】:
我想创建控制台应用程序,用于通过 Goolge Firebase 通知向不同的移动设备发送通知,
我已经看到链接Send push to Android by C# using FCM (Firebase Cloud Messaging)的代码
我收到内部服务器错误,状态码为 500
try{
string url = @"https://fcm.googleapis.com/fcm/send";
WebRequest tRequest = WebRequest.Create(url);
tRequest.Method = "post";
tRequest.ContentType = "application/json";
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + "This is the message" + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
var data = new
{
to = deviceId,
notification = new
{
body = "This is the message",
title = "This is the title"
}
};
string jsonss = Newtonsoft.Json.JsonConvert.SerializeObject(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonss);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/json";
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
Console.Write(sResponseFromServer);
}
}
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
{
var sss = ex.Message;
if (ex.InnerException != null)
{
var ss = ex.InnerException;
}
}
}
【问题讨论】:
-
从技术上讲,HTTP 500 不是你的错(毕竟是内部服务器错误 :))。但是,内部错误并非不可能由您的行为引起。另外,服务器的响应体有什么吗?
-
服务器 500 是内部服务器错误,但很可能是由客户端发送的无效数据引起的。
-
是的,Duston 是对的,我在变量 'applicationID' 中使用了错误值)
标签: c# firebase-cloud-messaging