问题描述
我在我的Android和iOS应用程序中使用FCM。客户端代码工作正常,因为从Firebase控制台我可以发送通知到两个平台没有任何问题。使用我的C#代码,我可以成功地向Android设备发送通知,但通知不会出现在iPhone上,除非直接来自Firebase通知控制台。 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 =这是消息,
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(StreamReader tReader = new StreamReader(dataStreamResponse))使用(WebResponse tResponse = tRequest.GetResponse())
{
using(Stream dataStreamResponse = tResponse.GetResponseStream())$ b $
{
String sResponseFromServer = tReader.ReadToEnd();
Response.Write(sResponseFromServer);
$ catch(Exception ex)
Response.Write(ex.Message);
$ / code>
通知不能在我的服务器端代码的iPhone上工作,但我得到一个好来自Firebase的响应。
{
multicast_id:479608 XXXXXX529964,
success:1,
failure:0,
canonical_ids:0,
results:[{
message_id:0:1467935842135743%a13567c6a13567c6
} ]
}
任何帮助或建议将非常感激。
尝试在您的FCM请求中将优先级字段设置为高。
pre $ var $ data $ new
= deviceId
通知= new
{
body =This is the message,
title =This is the title,
icon =myicon
},
priority =high
};
请注意,在开发中使用高优先级是好的,但在生产中它应该只在用户预计会采取行动,如回复聊天消息。
I am using FCM in my Android and iOS app. The client side code is working correctly because from the Firebase console I can send notifications to both platforms with out any problem. With my C# code I can send notifications successfully to android devices but the notifications never appear on iPhone unless directly coming from the Firebase notification console. I don't know what gives.
C# server-side code
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);
}
Notifications are not working on iPhone with my server side code but I get a good response from Firebase.
{
"multicast_id": 479608 XXXXXX529964,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [{
"message_id": "0:1467935842135743%a13567c6a13567c6"
}]
}
Any help or suggestions would be really appreciated.
Try setting the priority field to High in your FCM request.
Eg:
var data = new
{
to = deviceId,
notification = new
{
body = "This is the message",
title = "This is the title",
icon = "myicon"
},
priority = "high"
};
Note though that using high priority in development is fine but in production it should only be used when the user is expected to take action, like reply to a chat message.
这篇关于Firebase云消息传递和C#服务器端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!