Request FCM firebase cloud Message

0 thích 0 không thích
1 lượt xem
đã hỏi 22 Tháng 3, 2022 bởi nguyenthao (9,040 điểm)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SendCloudMessageFirebase
{
    public class Notification
    {
        public string body { get; set; }
        public string title { get; set; }
        public string click_action { get; set; }
        public string android_channel_id { get; set; }
    }

    public class Data
    {
        public string body { get; set; }
        public string title { get; set; }
        public string android_channel_id { get; set; }
        public string click_action { get; set; }
    }

    public class FirebaseRequestInfo
    {
        public Notification notification { get; set; }
        public Data data { get; set; }
        public string to { get; set; }
        public string priority { get; set; }
        public bool delay_while_idle { get; set; }
        public bool content_available { get; set; }
        public string android_channel_id { get; set; }
    }   
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SendCloudMessageFirebase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSendmessge_Click(object sender, EventArgs e)
        {
            SendMessage(txtTitle.Text, txtBody.Text);
        }
        public static string SendMessage(string title, string body)
        {           
            string serverKey = "xxxx";
            try
            {
                string result;
                var webAddr = "https://fcm.googleapis.com/fcm/send";

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add("Authorization", "Key=" + serverKey);
                httpWebRequest.Method = "POST";

                var firebaseRequestInfo = new FirebaseRequestInfo();

                var notification = new Notification();
                notification.body = body;
                notification.title = title;
                notification.click_action = "FLUTTER_NOTIFICATION_CLICK";
                notification.android_channel_id = "high_importance_channel";

                firebaseRequestInfo.notification = notification;

                var data = new Data();
                data.body = body;
                data.title = title;
                data.android_channel_id = "high_importance_channel";
                data.click_action = "FLUTTER_NOTIFICATION_CLICK";

                firebaseRequestInfo.data = data;

                firebaseRequestInfo.to = "/topics/EMS";
                firebaseRequestInfo.priority = "high";
                firebaseRequestInfo.delay_while_idle = true;
                firebaseRequestInfo.android_channel_id = "high_importance_channel";



                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    var dataRequest = JsonConvert.SerializeObject(firebaseRequestInfo);

                //    string data = $@"
                //       {{
                //        ""notification"": {{
                //                 ""body"": ""{body}"",
                //                  ""title"": ""{title}"",                              
                //                ""click_action"": ""FLUTTER_NOTIFICATION_CLICK"",
                //                ""android_channel_id"": ""high_importance_channel""

                //               }},

          
                //""data"": {{
                //            ""body"": ""{body}"",
                //            ""title"": ""{title}"",
                           
                //            ""android_channel_id"": ""high_importance_channel"",
                //            ""click_action"": ""FLUTTER_NOTIFICATION_CLICK"",
                           
                //          }},
                //          ""to"": ""/topics/EMS"",
                //          ""priority"" : ""high"",                         
                //          ""delay_while_idle"" : true,
                //          ""content_available"" : true, 
                //         ""android_channel_id"": ""high_importance_channel"",
                         
                //        }}
        
                //    ";
                 
                    streamWriter.Write(dataRequest);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();              
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }

                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return ex.ToString();
                
            }
        }

    }
}

    

1 câu trả lời

0 thích 0 không thích
đã trả lời 22 Tháng 3, 2022 bởi nguyenthao (9,040 điểm)
 
Câu trả lời hay nhất
Share cho anh em nào cần để test FCM (Firebase Cloud Message)
...