c# – 使用FCM和Asp.net web api 2
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用FCM和Asp.net web api 2,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2112字,纯文字阅读大概需要4分钟。
内容图文
![c# – 使用FCM和Asp.net web api 2](/upload/InfoBanner/zyjiaocheng/811/bc309ae887484cfbbd54ec3d66a50d98.jpg)
我已经构建了一个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; }
}
内容总结
以上是互联网集市为您收集整理的c# – 使用FCM和Asp.net web api 2全部内容,希望文章能够帮你解决c# – 使用FCM和Asp.net web api 2所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。