Sim*_*ode 4 c# push-notification asp.net-web-api firebase-cloud-messaging
我已经构建了一个web api,它将成为AngularJs,IOS和Android前端应用程序的后端.
现在我需要在例如产品更新时将通知从我的web api推送到前端应用程序.
我正在考虑使用SignalR以实时方式推送通知,但如果其他用户处于脱机状态则无用.
现在我打算使用FCM推送通知,所以你可以请你回答我的问题
如何将我的web api与FCM集成,以及在推送通知时使用FCM可以获得哪些好处?
PS
我将不胜感激任何将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; }
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2967 次 |
最近记录: |