我正在尝试使用FCM向Android手机发送一些通知。使用localhost和fiddler进行调试时,一切似乎都能正常工作。但是,当我尝试将我的Web服务部署到IIS,并用我的IP address/project directory/controller/action
替换localhost时,一切都开始分崩离析。
我的Webservice调用的方法:
public bool sendToFCMAndroid(string deviceToken, string message, string title, string type, string notificationId, string postingId)
{
List<dynamic> errorList = new List<dynamic>();
var data = new
{
to = deviceToken,
notification = new
{
body = message,
title = title
},
data = new
{
type = type,
notificationId = notificationId,
postingId = postingId
},
priority = "high"
};
try
{
HttpWebRequest tRequest = (HttpWebRequest)WebRequest.Create(new Uri(ConfigurationManager.AppSettings["urlFCM"]));
tRequest.Accept = "application/json";
tRequest.ContentType = "application/json";
tRequest.Method = "POST";
var json = JsonConvert.SerializeObject(data);
Byte[] byteArray = new ASCIIEncoding().GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", ConfigurationManager.AppSettings["androidFCMLegacyKey"]));
tRequest.Headers.Add(string.Format("Sender: id={0}", ConfigurationManager.AppSettings["androidFCMSenderId"]));
tRequest.ContentLength = byteArray.Length;
Stream newStream = tRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
WebResponse response = tRequest.GetResponse();
// Console.WriteLine(((HttpWebResponse)response).StatusDescription);
newStream = response.GetResponseStream();
StreamReader sr = new StreamReader(newStream);
FCMResponse jsonResp = JsonConvert.DeserializeObject<FCMResponse>(sr.ReadToEnd());
if (jsonResp.failure >= 1) errorList.Add(sr.ReadToEnd()); //check if got failures in json resp
//close everything use.
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
errorList.Add((ex.Message));
}
return (errorList.Count() == 0) ? true : false;
}
每当我通过ip / project-folder调用Webservice时,似乎总是返回false,所以我怀疑是发生了异常,或者FCM让我失败了。
编辑:我发现了一个潜在客户,在返回错误消息后,它显示The operation has timed out
。所以我的预感是在运行时触发了异常。现在的问题是,当我尝试在运行时发送到FCM而不是调试时间时,为什么它会超时?
EDIT2:我尝试将整个异常类作为JSON返回,这是我得到的两种不同类型的结果
这两个似乎发生在这条特定的线上,很奇怪。
Line 206: Stream newStream = tRequest.GetRequestStream();
Line 207: newStream.Write(byteArray, 0, byteArray.Length);