c# - 将 FCM 与 Asp.net web api 2 结合使用

标签 c# asp.net-web-api push-notification firebase-cloud-messaging

我已经构建了一个 web api,它将作为 AngularJsIOSAndroid 前端应用程序的后端。

现在我需要将通知从我的 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/

相关文章:

ios - 使用 Apple Push Notifications (APN) for Passbook 时 token 无效

c# - 400 BAD 请求尝试联系 Azure 异常检测 API

c# - Unity - 解析继承自 T 的泛型实现

c# - Controller 和 IDisposable

c# - 如何在 ASP.NET Web API 中设置下载文件名

push-notification - 使用 PHP 客户端为 Google Calendar API 设置推送通知

c# - 依赖注入(inject)容器 : how to handle objects that aren't a dependency of anything?

c# - C#/.NET 编译器不警告 Dispose() 有什么原因吗?

c# - 将 gmail api 与 asp.net web api 结合使用

ios - 让用户使用 parse.com 的云代码关闭不同的通知类型