C#-向FCM Firebase发送通知失败
我正在尝试使用FCM向Android手机发送一些通知。当使用localhost和fiddler进行调试时,一切似乎都正常工作。但是,当我尝试将Web服务部署到IIS,并用我的C#-向FCM Firebase发送通知失败,c#,android,firebase,firebase-cloud-messaging,C#,Android,Firebase,Firebase Cloud Messaging,我正在尝试使用FCM向Android手机发送一些通知。当使用localhost和fiddler进行调试时,一切似乎都正常工作。但是,当我尝试将Web服务部署到IIS,并用我的IP地址/project目录/controller/action替换本地主机时,一切都开始崩溃 我的Web服务调用的方法: public bool sendToFCMAndroid(string deviceToken, string message, string title, string type, string no
IP地址/project目录/controller/action
替换本地主机时,一切都开始崩溃
我的Web服务调用的方法:
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;
}
你好很难对原因进行评论。关于它的来源几乎没有任何细节。您是否有可能获得准确的错误响应(如果它来自FCM)?我突然想到,在运行时通过IIS调用它而不仅仅是ex.Message调用它时,jsonify会抛出整个异常。我周一回办公室试试。可能会对matterHi@AL有更多的了解。我发布了一个更新,看看是否有帮助。这个连接在你们学校内部?然后,与FCM的连接可能被阻塞。你看到那张纸条了吗?--“如果您的组织有防火墙限制进出Internet的流量,您需要将其配置为允许与FCM连接,以便您的Firebase云消息客户端应用程序接收消息。要打开的端口为:5228、5229和5230。FCM通常只使用5228,但有时使用5229和5230。”奇怪的是,当我使用
http:/localhost
时,一切正常,而当我将其部署到IIShttp://xxxx.xxxx.xxxx.xxxx/iis-sub-directory/
Hi。很难对原因进行评论。关于它的来源几乎没有任何细节。您是否有可能获得准确的错误响应(如果它来自FCM)?我突然想到,在运行时通过IIS调用它而不仅仅是ex.Message调用它时,jsonify会抛出整个异常。我周一回办公室试试。可能会对matterHi@AL有更多的了解。我发布了一个更新,看看是否有帮助。这个连接在你们学校内部?然后,与FCM的连接可能被阻塞。你看到那张纸条了吗?--“如果您的组织有防火墙限制进出Internet的流量,您需要将其配置为允许与FCM连接,以便您的Firebase云消息客户端应用程序接收消息。要打开的端口为:5228、5229和5230。FCM通常只使用5228,但有时使用5229和5230。”奇怪的是,当我使用http:/localhost
时,一切正常,而当我将其部署到IIShttp://xxxx.xxxx.xxxx.xxxx/iis-sub-directory/
Line 206: Stream newStream = tRequest.GetRequestStream();
Line 207: newStream.Write(byteArray, 0, byteArray.Length);