Firebase 云消息传递和 C# 服务器端代码
Firebase Cloud Messaging and C# server side code
我在 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 = "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 上的通知不起作用,但我从 Firebase 得到了很好的响应。
{
"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"
};
请注意,虽然在开发中使用高优先级很好,但在生产中它应该只在用户需要采取行动时使用,比如回复聊天消息。
我在 android 和 IOS 中使用 FCM 推送 notification.I 我正在使用 Visual Studio 2015.To 创建一个网络 api 项目并且a 添加一个 controller.write code.Code 下面给出
using System;
using System.Net;
using System.Web.Http;
using System.Web.Script.Serialization;
using System.Configuration;
using System.IO;
namespace pushios.Controllers
{
public class HomeController : ApiController
{
[HttpGet]
[Route("sendmessage")]
public IHttpActionResult SendMessage()
{
var data = new {
to = "device Tokens", // iphone 6s test token
data = new
{
body = "test",
title = "test",
pushtype="events",
},
notification = new {
body = "test",
content_available = true,
priority= "high",
title = "C#"
}
} ;
SendNotification(data);
return Ok();
}
public void SendNotification(object data)
{
var Serializer = new JavaScriptSerializer();
var json = Serializer.Serialize(data);
Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);
SendNotification(byteArray);
}
public void SendNotification(Byte[] byteArray)
{
try
{
String server_api_key = ConfigurationManager.AppSettings["SERVER_API_KEY"];
String senderid = ConfigurationManager.AppSettings["SENDER_ID"];
WebRequest type = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
type.Method = "post";
type.ContentType = "application/json";
type.Headers.Add($"Authorization: key={server_api_key}");
type.Headers.Add($"Sender: id={senderid}");
type.ContentLength = byteArray.Length;
Stream datastream = type.GetRequestStream();
datastream.Write(byteArray, 0, byteArray.Length);
datastream.Close();
WebResponse respones = type.GetResponse();
datastream = respones.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
String sresponessrever = reader.ReadToEnd();
reader.Close();
datastream.Close();
respones.Close();
}
catch (Exception)
{
throw;
}
}
}
}
在android的情况下给出json
var data = new {
to = "device Tokens", // iphone 6s test token
data = new
{
body = "test",
title = "test",
pushtype="events",
};
在IOS的情况下json
var data = new {
to = "device Tokens", // iphone 6s test token
data = new
{
body = "test",
title = "test",
pushtype="events",
},
notification = new {
body = "test",
content_available = true,
priority= "high",
title = "C#"
}
} ;
SERVER_API_KEY,SENDER_ID 我正在添加网络 config.To 在 FCM 中收集 SERVER_API_KEY,SENDER_ID。
<add key="SERVER_API_KEY" value="ADD Key in FCM"/>
<add key="SENDER_ID" value="Add key in fcm "/>
我在 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 = "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 上的通知不起作用,但我从 Firebase 得到了很好的响应。
{
"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"
};
请注意,虽然在开发中使用高优先级很好,但在生产中它应该只在用户需要采取行动时使用,比如回复聊天消息。
我在 android 和 IOS 中使用 FCM 推送 notification.I 我正在使用 Visual Studio 2015.To 创建一个网络 api 项目并且a 添加一个 controller.write code.Code 下面给出
using System;
using System.Net;
using System.Web.Http;
using System.Web.Script.Serialization;
using System.Configuration;
using System.IO;
namespace pushios.Controllers
{
public class HomeController : ApiController
{
[HttpGet]
[Route("sendmessage")]
public IHttpActionResult SendMessage()
{
var data = new {
to = "device Tokens", // iphone 6s test token
data = new
{
body = "test",
title = "test",
pushtype="events",
},
notification = new {
body = "test",
content_available = true,
priority= "high",
title = "C#"
}
} ;
SendNotification(data);
return Ok();
}
public void SendNotification(object data)
{
var Serializer = new JavaScriptSerializer();
var json = Serializer.Serialize(data);
Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);
SendNotification(byteArray);
}
public void SendNotification(Byte[] byteArray)
{
try
{
String server_api_key = ConfigurationManager.AppSettings["SERVER_API_KEY"];
String senderid = ConfigurationManager.AppSettings["SENDER_ID"];
WebRequest type = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
type.Method = "post";
type.ContentType = "application/json";
type.Headers.Add($"Authorization: key={server_api_key}");
type.Headers.Add($"Sender: id={senderid}");
type.ContentLength = byteArray.Length;
Stream datastream = type.GetRequestStream();
datastream.Write(byteArray, 0, byteArray.Length);
datastream.Close();
WebResponse respones = type.GetResponse();
datastream = respones.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
String sresponessrever = reader.ReadToEnd();
reader.Close();
datastream.Close();
respones.Close();
}
catch (Exception)
{
throw;
}
}
}
}
在android的情况下给出json
var data = new {
to = "device Tokens", // iphone 6s test token
data = new
{
body = "test",
title = "test",
pushtype="events",
};
在IOS的情况下json
var data = new {
to = "device Tokens", // iphone 6s test token
data = new
{
body = "test",
title = "test",
pushtype="events",
},
notification = new {
body = "test",
content_available = true,
priority= "high",
title = "C#"
}
} ;
SERVER_API_KEY,SENDER_ID 我正在添加网络 config.To 在 FCM 中收集 SERVER_API_KEY,SENDER_ID。
<add key="SERVER_API_KEY" value="ADD Key in FCM"/>
<add key="SENDER_ID" value="Add key in fcm "/>