我已经构建了一个 web api,它将作为 AngularJs、IOS 和 Android 前端应用程序的后端。
现在我需要将通知从我的 web api 推送到前端应用程序,例如当产品已更新时。
我正在考虑使用 SignalR 以实时方式推送通知,但如果其他用户离线,它就没有用了。
现在我打算使用 FCM 来推送通知所以你能不能请你回答我的问题
如何将我的网络 API 与 FCM 集成以及使用 FCM 推送通知有哪些好处?
附言
如果有任何将 asp.net web api 与 FCM 集成的引用资料,我将不胜感激
最佳答案
让我们创建控制台应用程序如下:
class Program
{
static void Main(string[] args)
{
string resend ;
do
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
{
to = "Token the device you want to push notification to",
data = new
{
title = "title",
body = "body",
icon = "/firebase-logo.png"
}
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", "your authorization key"));
tRequest.Headers.Add(string.Format("Sender: id={0}", "your senderId"));
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/json";
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 responseFromFirebaseServer = tReader.ReadToEnd();
FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
if (response.success == 1)
{
Console.WriteLine("succeeded");
}
else if (response.failure == 1)
{
Console.WriteLine("failed");
}
}
}
}
}
resend = Console.ReadLine();
} while (resend == "c");
}
}
public class FCMResponse
{
public long multicast_id { get; set; }
public int success { get; set; }
public int failure { get; set; }
public int canonical_ids { get; set; }
public List<FCMResult> results { get; set; }
}
public class FCMResult
{
public string message_id { get; set; }
}
关于c# - 将 FCM 与 Asp.net web api 2 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48706087/