Firebase云消息传递和C#服务器端代码
问题描述:
我在我的Android和iOS应用程序中使用FCM。客户端代码工作正常,因为通过Firebase控制台,我可以向两个平台发送没有任何问题的通知。通过我的C#代码,我可以成功地向Android设备发送通知,但通知不会出现在iPhone上,除非直接来自Firebase通知控制台。我不知道是什么给了。Firebase云消息传递和C#服务器端代码
C#服务器端代码
try
{
var applicationID = "application_id";
var senderId = "sender_id";
string deviceId = "device_id_of_reciever";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceId,
notification = new
{
body = "This is the message",
title = "This is the title",
icon = "myicon"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
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();
Response.Write(sResponseFromServer);
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
通知不工作在iPhone上我的服务器端代码,但我从火力地堡了良好的反响。
{
"multicast_id": 479608 XXXXXX529964,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [{
"message_id": "0:1467935842135743%a13567c6a13567c6"
}]
}
任何帮助或建议将非常感激。
答
尝试在您的FCM请求中将优先级字段设置为高。
如:
var data = new
{
to = deviceId,
notification = new
{
body = "This is the message",
title = "This is the title",
icon = "myicon"
},
priority = "high"
};
注意,虽然在发展,使用高优先级是好的,但在生产中,当用户预计将采取行动,就像回复聊天消息,它应该只被使用。
+0
我标记答案正确。就我而言,我必须将优先级设置为10(优先级= 10),然后一切正常。提供的答案是非常有用的。谢谢亚瑟 –
这些消息直接来自Firebase时看起来有所不同。 gcm.message_id:0:1467938951987907%a13567c6XXXX67c6] %@ [aps:{alert = { body =“这是消息”; title =“这是标题”; }; },gcm.message_id:0:14679XXXX87907%a13567c6a1XXXXc6] 消息收到 消息ID:0:1467939XXXXX5%a13567c6a13567c6 %@ [google.c.a.c_l:why2,google.c.a.e:1,APS:{ 警报= why1; },gcm.ne:1,google.cac_id:50607XXXXXX957125,google.caudt:0,gcm.message_id:0:14679XXXX18215%a13567c6a1XXX7c6,google.cats:146XXXXX32] –
嗨,你能告诉我如何发送deviceId ? –